<?php
namespace Mobile\Controller;
use Think\Controller;
class ProjectController extends CommunalController{
	//项目列表 designed by:zhangsen 2017-09-29
	function project_list(){
		if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
		$this->assign('center_id',$center_id);
		if(!empty($_REQUEST['p_type'])){$p_type=$_REQUEST['p_type'];}else{$p_type=-10;}
		$this->assign('p_type',$p_type);
		$db_project_seach=M('db_project_seach'); 
		//列表
		$db_project=M('db_project');
		$string=NULL;
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['manager_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['director_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
			$add_where['center_id']=$center_id;
			$add_where['keyword']=$_REQUEST['keyword'];
			$add_where['add_uid']=$this->login_id;
            $select_res=$db_project_seach->where($add_where)->find();
            if(empty($select_res)){
                $add_where['add_date']=date('Y-m-d H:i:s');
                $db_project_seach->add($add_where);
            }
		}
		if($_REQUEST['center_id']=='130003'){
				if($p_type!=-10 and !get_permission('SW',1)){$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';}
			if(get_permission('SW',1)){
				
			}elseif(get_permission('SW',2)){
				if(!in_array($center_id,$this->login_select_id)){$string.="1=2 or";}
			}elseif(get_permission('SW',3)){
				$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';
			}else{
				$string.="1=2 or";
			}
		}else{
			if($p_type!=-10 and !get_permission('XM',1)){$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';}
			if(get_permission('XM',1)){
				
			}elseif(get_permission('XM',2)){
				if(!in_array($center_id,$this->login_select_id)){$string.="1=2 or";}
			}elseif(get_permission('XM',3)){
				$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';
			}else{
				$string.="1=2 or";
			}
		}
		if($this->get_is_project_function()){
			$db_project_function=M('db_project_function'); 
			$where_f['uid']=$this->login_id;
			$where_f['start_date']=array('elt',date("Y-m-d"));
			$where_f['end_date']=array('egt',date("Y-m-d"));
			$result_f=$db_project_function->where($where_f)->select();
			foreach($result_f as $key=>$value){
				$pids.=$value['pid'].',';
			}
			$string.="(id in (".rtrim($pids,',').")) or";
		}
		if($string){$where['_string']=rtrim($string,'or');}
		if($center_id!=-10){$where['db_project.center_id']=$center_id;}else{$where['db_project.center_id']=array('not in',"130003,130005,130004");}
		if($center_id!=130003){
			$where['status']=1;
		}
		$result=$db_project->field('db_project.*')->where($where)->order('db_project.id desc')->limit(6)->select();
		$count=$db_project->field('db_project.id')->where($where)->count();
		foreach($result as $key=>$value)
		{	
			//区分拓展项目
			if($center_id==130003){
				$lb=0;
				//10%-项目启动表提交
				$project_bg=$this->get_project_bg($value['id']);
				if($project_bg['status']==1){
					$lb=10;
					$str="<span data-toggle='tooltip' data-placement='right' title='项目信息完成'>".$lb."%</span>";
				}
				//20%-前期沟通中有实际数据
				$db_project_communicate=M('db_project_communicate');
				$where_jq['pid']=$value['id'];
				$project_jq=$db_project_communicate->where($where_jq)->select();
				if(!empty($project_jq)){
					$lb=20;
				}
				//40%-商务谈判阶段有考察或接待数据
				//50%-商务谈判阶段有工作建议书记录
				$db_project_negotiation=M('db_project_negotiation');
				$where_n['pid']=$value['id'];
				$project_n=$db_project_negotiation->where($where_n)->select();
				if(!empty($project_n)){
					foreach($project_n as $k=>$v){
						if(preg_match('/工作建议书/',$v['type'])){
							$lb=50;
							break;
						}
						elseif(preg_match('/考察/',$v['type']) or preg_match('/接待/',$v['type'])){
							$lb=40;
						}
					}
				}
				//60%-商务签约环节已提交框架协议
				if(!empty($value['is_contract']) and $value['qy_status']==1){
					$lb=60;
				}
				//90%-商务签约环节已签约选项为“是”
				if($value['is_sign']=='是' and $value['qy_status']==2){
					$lb=90;
				}
				//100%-首款已到选项为完成
				if($value['is_first_money']=='是'){
					$lb=100;
				}
				$result[$key]['lb']=$lb;
			}
			if($_REQUEST['keyword']){
				$result[$key]['project_num']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_num']);
				$result[$key]['project_name']=preg_replace("/".$_REQUEST['keyword']."/", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_name']);
				$result[$key]['manager_name']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['manager_name']);
				$result[$key]['director_name']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['director_name']);
			}
		}
		$this->assign('result',$result);
		$this->assign('count',$count);
		//推荐搜索
		$where_s['center_id']=$center_id;
		$result_t=$db_project_seach->field('keyword,count(id) as ct')->where($where_s)->group('center_id,keyword')->order('ct desc,add_date desc')->limit(6)->select();
		$this->assign('result_t',$result_t);
		//搜索历史
		$where_l['center_id']=$center_id;
		$where_l['add_uid']=$this->login_id;
		$result_l=$db_project_seach->field('keyword,max(add_date) as md')->where($where_l)->group('center_id,keyword')->order('md desc')->limit(6)->select();
		$this->assign('result_l',$result_l);
		$this->display();
	}
	
	//项目列表-->下拉加载 designed by:zhangsen 2017-09-29
	public function project_list_ajax(){
		if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
		$start=$_REQUEST['start_size'];
		$end=$_REQUEST['start_page'];
		if(!empty($_REQUEST['p_type'])){$p_type=$_REQUEST['p_type'];}else{$p_type=-10;}
		$this->assign('p_type',$p_type);
		$db_project_seach=M('db_project_seach'); 
		$db_project=M('db_project');
		$string=NULL;
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['project_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['manager_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['director_name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
		}
		if($_REQUEST['center_id']=='130003'){
			if($p_type!=-10 and !get_permission('SW',1)){$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';}
			if(get_permission('SW',1)){
				
			}elseif(get_permission('SW',2)){
				if(!in_array($center_id,$this->login_select_id)){$string.="1=2 or";}
			}elseif(get_permission('SW',3)){
				$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';
			}else{
				$string.="1=2 or";
			}
		}else{
			if($p_type!=-10 and !get_permission('XM',1)){$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';}
			if(get_permission('XM',1)){
				
			}elseif(get_permission('XM',2)){
				if(!in_array($center_id,$this->login_select_id)){$string.="1=2 or";}
			}elseif(get_permission('XM',3)){
				$string.='(add_uid='.$this->login_id.' or manager_uid='.$this->login_id.' or director_uid='.$this->login_id.') or';
			}else{
				$string.="1=2 or";
			}
		}
		if($this->get_is_project_function()){
			$db_project_function=M('db_project_function'); 
			$where_f['uid']=$this->login_id;
			$where_f['start_date']=array('elt',date("Y-m-d"));
			$where_f['end_date']=array('egt',date("Y-m-d"));
			$result_f=$db_project_function->where($where_f)->select();
			foreach($result_f as $key=>$value){
				$pids.=$value['pid'].',';
			}
			$string.="(id in (".rtrim($pids,',').")) or";
		}
		if($string){$where['_string']=rtrim($string,'or');}
		if($center_id!=-10){$where['db_project.center_id']=$center_id;}else{$where['db_project.center_id']=array('not in',"130003,130005,130004");}
		if($center_id!=130003){
			$where['status']=1;
		}
		$data=$db_project->where($where)->order('db_project.id desc')->limit($start,$end)->select();
		//区分拓展项目
		foreach($data as $key=>$value)
		{	
			if($center_id==130003){
				$lb=0;
				//10%-项目启动表提交
				$project_bg=$this->get_project_bg($value['id']);
				if($project_bg['status']==1){
					$lb=10;
				}
				//20%-前期沟通中有实际数据
				$db_project_communicate=M('db_project_communicate');
				$where_jq['pid']=$value['id'];
				$project_jq=$db_project_communicate->where($where_jq)->select();
				if(!empty($project_jq)){
					$lb=20;
				}
				//40%-商务谈判阶段有考察或接待数据
				//50%-商务谈判阶段有工作建议书记录
				$db_project_negotiation=M('db_project_negotiation');
				$where_n['pid']=$value['id'];
				$project_n=$db_project_negotiation->where($where_n)->select();
				if(!empty($project_n)){
					foreach($project_n as $k=>$v){
						if(preg_match('/工作建议书/',$v['type'])){
							$lb=50;
							break;
						}
						elseif(preg_match('/考察/',$v['type']) or preg_match('/接待/',$v['type'])){
							$lb=40;
						}
					}
				}
				//60%-商务签约环节已提交框架协议
				if(!empty($value['is_contract']) and $value['qy_status']==1){
					$lb=60;
				}
				//90%-商务签约环节已签约选项为“是”
				if($value['is_sign']=='是' and $value['qy_status']==2){
					$lb=90;
				}
				//100%-首款已到选项为完成
				if($value['is_first_money']=='是'){
					$lb=100;
				}
				$data[$key]['lb']=$lb;
			}
			if($_REQUEST['keyword']){
				$data[$key]['project_num']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_num']);
				$data[$key]['project_name']=preg_replace("/".$_REQUEST['keyword']."/", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_name']);
				$data[$key]['manager_name']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['manager_name']);
				$data[$key]['director_name']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['director_name']);
			}
		}
		echo json_encode($data);die;
	}
	
	//项目列表-->清空历史操作 designed by:zhangsen 2017-12-22
	public function project_list_status(){
		$db_project_seach=M('db_project_seach'); 
		// $where['center_id']=$_REQUEST['center_id'];
		$where['add_uid']=$this->login_id;
		$db_project_seach->where($where)->delete();
	}
	
	//运营周报 designed by:zhaoshihao 2017-09-25
	function project_operate_list(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		$this->assign('project_bg',$project_bg);
		//列表
		$db_project_operate=M('db_project_operate');
		$where['db_project_operate.pid']=$_REQUEST['id'];
		$result=$db_project_operate->where($where)->limit(10)->order('year desc,week desc')->select();
		$db_project_operate_answer=M('db_project_operate_answer');
		foreach($result as $key=>$value){
			//添加人
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			//获得周数
			$week=$this->get_year_month_week(6,$value['year'],$value['week']);
			if($value['type']==2){
				//获得节假日
				$holiday=$this->get_holiday($value['year'],$value['holiday']);
				$result[$key]['week']=$holiday['name'];
				//$result[$key]['week_detail']=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";
				$result[$key]['week_detail']=$value['year']."年".substr($holiday['start_date'],5,2).".".substr($holiday['start_date'],8,2)."~".substr($holiday['end_date'],5,2).".".substr($holiday['end_date'],8,2);
							
			}else{
				$result[$key]['week']=$value['week'].'周';
				$result[$key]['week_detail']=$value['year']."年".substr($week[0],5,2).".".substr($week[0],8,2)."~".substr($week[6],5,2).".".substr($week[6],8,2);
			}
			//是否有批复
			$where_m['mid']=$value['id'];
			$message=$db_project_operate_answer->where($where_m)->order('id desc')->find();
			if(!empty($message)){
				$result[$key]['audit_user']=$this->get_username($message['add_uid']);
				$result[$key]['audit_date']=$message['add_date'];
			}
		}
		$count=$db_project_operate->where($where)->count();
		$this->assign('count',$count);
		$this->assign('result',$result);
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}
	
	//运营周报-->下拉加载 designed by:zhaoshihao 2017-09-25
	function project_operate_list_ajax(){
		$start=$_REQUEST['start_size'];
		$end=$_REQUEST['start_page'];
		//列表
		$db_project_operate=M('db_project_operate');
		$where['db_project_operate.pid']=$_REQUEST['id'];
		$data=$db_project_operate->where($where)->limit($start,$end)->order('year desc,week desc')->select();
		$db_project_operate_answer=M('db_project_operate_answer');
		foreach($data as $key=>$value){
			//添加人
			$data[$key]['add_user']=$this->get_username($value['add_uid']);
			//获得周数
			$week=$this->get_year_month_week(6,$value['year'],$value['week']);
			if($value['type']==2){
				//获得节假日
				$holiday=$this->get_holiday($value['year'],$value['holiday']);
				$data[$key]['week']=$holiday['name'];
				$data[$key]['week_detail']=$value['year']."年".substr($holiday['start_date'],5,2).".".substr($holiday['start_date'],8,2)."~".substr($holiday['end_date'],5,2).".".substr($holiday['end_date'],8,2);
							
			}else{
				$data[$key]['week']=$value['week'].'周';
				$data[$key]['week_detail']=$value['year']."年".substr($week[0],5,2).".".substr($week[0],8,2)."~".substr($week[6],5,2).".".substr($week[6],8,2);
			}
			//是否有批复
			$where_m['mid']=$value['id'];
			$message=$db_project_operate_answer->where($where_m)->order('id desc')->find();
			if(!empty($message)){
				$data[$key]['audit_user']=$this->get_username($message['add_uid']);
				$data[$key]['audit_date']=$message['add_date'];
			}
		}
		echo json_encode($data);die;
	}
	
	//运营周报-->添加第一步 designed by:zhaoshihao 2017-09-25
	function project_operate_add_zq(){
		//项目详情
		$project=parent::get_project($_REQUEST['pid']);
		$this->assign('project',$project);
		$this->assign('year',date("Y"));
		$this->assign("year_list",$this->get_year_month_week(1));
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['pid']);
		$this->assign('project_bg',$project_bg);
		//获得周数
		$db_project_operate=M('db_project_operate');
		$where_week['pid']=$_REQUEST['pid'];
		$where_week['year']=date("Y");
		$where_week['type']=1;
		$week_exist=$db_project_operate->field('week')->where($where_week)->order('week desc')->select();
		$week_exist=$this->kg_array_values($week_exist,'week');
		$where_h['pid']=$_REQUEST['pid'];
		$where_h['year']=date("Y");
		$where_h['type']=2;
		$holiday_exist=$db_project_operate->field('holiday')->where($where_h)->order('week desc')->select();
		$holiday_exist=$this->kg_array_values($holiday_exist,'holiday');
		$whole_week=$this->get_year_month_week(5,date("Y"));
		$db_holiday=M('db_holiday');
		for($i=1;$i<=$whole_week;$i++)
		{
			if(!in_array($i,$week_exist))
			{
				$str.=$i.',';
			}	
			$where_f['year']=date("Y");
			$where_f['week']=$i;
			$result_f=$db_holiday->where($where_f)->find();
			if(!empty($result_f)){
				if(!in_array($result_f['name'],$holiday_exist))
				{
					$str.=$result_f['name'].',';
				}
			}
		}
		$str_arr=explode(',',rtrim($str,','));
		$week_list=array();
		foreach($str_arr as $key=>$value){
			$holiday=$this->get_holiday(date("Y"),$value);
			if($holiday){
				$week_list[$key]['num']=$holiday['week'];
				$holiday=$this->get_holiday(date("Y"),$value);
				$week_list[$key]['detail']=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";
				$week_list[$key]['holiday']=$value;		
			}
			else{
				$week_list[$key]['num']=$value;
				$week=$this->get_year_month_week(6,date("Y"),$value);
				$week_list[$key]['detail']="第".$value."周（".$week[0]."至".$week[6]."）";
			}
		}
		krsort($week_list);
		$b=4;
		foreach($week_list as $k1=>$v1){
			if($b!=0){
				if($v1['num']<date("W")){
					if(date("W")-4<$v1['num']){
						$weeks[]=$week_list[$k1];
						unset($week_list[$kk]);
						$b--;
					}
				}
			}
		}
		if($b>0){
			$str=NULL;
			$where_weekb['pid']=$_REQUEST['pid'];
			$where_weekb['year']=date("Y")-1;
			$where_weekb['type']=1;
			$week_existss=$db_project_operate->field('week')->where($where_weekb)->order('week desc')->select();
			$week_existss=$this->kg_array_values($week_existss,'week');
			$where_hb['pid']=$_REQUEST['pid'];
			$where_hb['year']=date("Y")-1;
			$where_hb['type']=2;
			$holiday_existss=$db_project_operate->field('holiday')->where($where_hb)->order('week desc')->select();
			$holiday_existss=$this->kg_array_values($holiday_existss,'holiday');
			$whole_week=$this->get_year_month_week(5,date("Y")-1);
			$db_holiday=M('db_holiday');
			for($i=1;$i<=$whole_week;$i++)
			{
				if($whole_week+date("W")-4<=$i){
					if(!in_array($i,$week_existss))
					{
						$strb.=$i.',';
					}	
					$where_f['year']=date("Y")-1;
					$where_f['week']=$i;
					$result_f=$db_holiday->where($where_f)->find();
					if(!empty($result_f)){
						if(!in_array($result_f['name'],$holiday_existss))
						{
							$strb.=$result_f['name'].',';
						}
					}
				}
			}
			if($strb){$str_arrb=explode(',',rtrim($strb,','));}
			foreach($str_arrb as $key=>$value){
				$holiday=$this->get_holiday(date("Y")-1,$value);
				if($holiday){
					$bb[$key]['num']=$holiday['week'];
					$holiday=$this->get_holiday(date("Y")-1,$value);
					$bb[$key]['detail']=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";	
					$bb[$key]['holiday']=$value;	
				}
				else{
					$bb[$key]['num']=$value;
					$week=$this->get_year_month_week(6,date("Y")-1,$value);
					$bb[$key]['detail']="第".$value."周（".$week[0]."至".$week[6]."）";
				}
				$weeks[]=$bb[$key];
				$b--;
			}
		}
		$weeks=array_reverse($weeks);
		$week_list=array_reverse($week_list);
		$a=$b+4;
		foreach($week_list as $kk=>$vv){
			if($a!=0){
				if($vv['num']>=date("W")){
					$weeks[]=$week_list[$kk];
					unset($week_list[$kk]);
					$a--;
				}
			}
		}
		if($a>0){
			$str=NULL;
			$where_weeka['pid']=$_REQUEST['pid'];
			$where_weeka['year']=date("Y")+1;
			$where_weeka['type']=1;
			$week_exists=$db_project_operate->field('week')->where($where_weeka)->order('week desc')->select();
			$week_exists=$this->kg_array_values($week_exists,'week');
			$where_ha['pid']=$_REQUEST['pid'];
			$where_ha['year']=date("Y")+1;
			$where_ha['type']=2;
			$holiday_exists=$db_project_operate->field('holiday')->where($where_ha)->order('week desc')->select();
			$holiday_exists=$this->kg_array_values($holiday_exists,'holiday');
			$whole_week=$this->get_year_month_week(5,date("Y")+1);
			$db_holiday=M('db_holiday');
			for($i=1;$i<=$whole_week;$i++)
			{
				if(!in_array($i,$week_exists))
				{
					$str.=$i.',';
				}	
				$where_f['year']=date("Y")+1;
				$where_f['week']=$i;
				$result_f=$db_holiday->where($where_f)->find();
				if(!empty($result_f)){
					if(!in_array($result_f['name'],$holiday_exists))
					{
						$str.=$result_f['name'].',';
					}
				}
			}
			$str_arr=explode(',',rtrim($str,','));
			$aa=array();
			foreach($str_arr as $key=>$value){
				if($a!=0)
				{
					$holiday=$this->get_holiday(date("Y")+1,$value);
					if($holiday){
						$week_list[$key]['num']=$holiday['week'];
						$holiday=$this->get_holiday(date("Y")+1,$value);
						$week_list[$key]['detail']=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";
						$week_list[$key]['holiday']=$value;	
					}
					else{
						$week_list[$key]['num']=$value;
						$week=$this->get_year_month_week(6,date("Y")+1,$value);
						$week_list[$key]['detail']="第".$value."周（".$week[0]."至".$week[6]."）";
					}
					$weeks[]=$week_list[$key];
					$a--;
				}
				else{
					break;
				}
			}
		}
		$this->assign('week_list',$weeks);
		$this->display();
	}
	
	//运营周报-->添加第二步 designed by:zhaoshihao 2017-09-25
	function project_operate_add_hb(){
		//获得周数
		$holiday=$this->get_holiday(date("Y"),$_REQUEST['week']);
		if($holiday){
			$week_detail=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";	
		}
		else{
			$week=$this->get_year_month_week(6,date("Y"),$_REQUEST['week']);
	 		$week_detail="第".$_REQUEST['week']."周（".$week[0]."至".$week[6]."）";
		}
		$this->assign('week_detail',$week_detail);
		$gzhb=$this->library_type_list(7,32,NULL,1);
		$i=0;
		foreach($gzhb as $key=>$val){
			foreach($val['second'] as $kk=>$vv){
				if(empty($vv['third'])){
					$gzhb[$key]['second'][$kk]['num']=$i;
					$i++;
				}else{
					foreach($vv['third'] as $k1=>$v1){
						$gzhb[$key]['second'][$kk]['third'][$k1]['num']=$i;
						$i++;
					}
				}
			}
		}
		$this->assign('gzhb',$gzhb);
		$this->assign('week',$_REQUEST['week']);
		$this->assign('pid',$_REQUEST['pid']);
		$this->display();
	}
	
	//运营周报-->编辑第二步 designed by:zhaoshihao 2017-09-25
	function project_operate_edit_hb(){
		//详情
		$db_project_operate=M('db_project_operate');
		$where_find['id']=$_REQUEST['mid'];
		$operate=$db_project_operate->where($where_find)->find();
		$this->assign('pid',$operate['pid']);
		//获得周数
		if($operate['type']==2){
			//获得节假日
			$holiday=$this->get_holiday($operate['year'],$operate['holiday']);
			$week_detail=$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";
						
		}else{
			$week=$this->get_year_month_week(6,$operate['year'],$operate['week']);
			$week_detail="第".$operate['week']."周（".$week[0]."至".$week[6]."）";
		}
		$this->assign('week_detail',$week_detail);
		//工作汇报
		$db_project_operate_detail=M('db_project_operate_detail');
		$where_detail['mid']=$_REQUEST['mid'];
		$where_detail['fid']=32;
		$gzhb_detail=$db_project_operate_detail->where($where_detail)->order('sid,tid')->select();
		foreach($gzhb_detail as $key=>$value){
			$gzhb_detail[$key]['second_name']=$this->get_db_universal_label(2,$value['sid']);
			$gzhb_detail[$key]['third_name']=$this->get_db_universal_label(3,$value['tid']);
			if(empty($value['tid'])){$two[]=$value['sid'];}else{$three[]=$value['tid'];}
		}
		$gzhb=$this->library_type_list(7,32,NULL,1);
		$i=0;
		foreach($gzhb as $key=>$val){
			foreach($val['second'] as $kk=>$vv){
				if(empty($vv['third'])){
					if(in_array($vv['id'],$two)){
						$gzhb[$key]['second'][$kk]['choose']=1;
					}
					$gzhb[$key]['second'][$kk]['num']=$i;
					$i++;
				}else{
					foreach($vv['third'] as $k1=>$v1){
						if(in_array($v1['id'],$three)){
							$gzhb[$key]['second'][$kk]['third'][$k1]['choose']=1;
						}
						$gzhb[$key]['second'][$kk]['third'][$k1]['num']=$i;
						$i++;
					}
				}
			}
		}
		foreach($gzhb_detail as $k2=>$v2){
			foreach($gzhb as $key=>$val){
				foreach($val['second'] as $kk=>$vv){
					if(empty($v2['tid'])){
						if($v2['sid']==$vv['id']){
							$gzhb_detail[$k2]['size']=$vv['num'];
						}
					}else{
						foreach($vv['third'] as $k1=>$v1){
							if($v2['tid']==$v1['id']){
								$gzhb_detail[$k2]['size']=$v1['num'];
							}
						}
					}
				}
			}	
		}
		$this->assign('gzhb_detail',$gzhb_detail);
		$this->assign('gzhb',$gzhb);
		$this->assign('mid',$_REQUEST['mid']);
		$this->display();
	}
	
	//运营周报-->详情 designed by:zhaoshihao 2017-09-25
	function project_operate_detail(){
		//项目详情
		$project=parent::get_project($_REQUEST['pid']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['pid']);
		$this->assign('project_bg',$project_bg);
		//详情
		$db_project_operate=M('db_project_operate');
		$where_find['id']=$_REQUEST['id'];
		$operate=$db_project_operate->where($where_find)->find();
		$operate['add_user']=$this->get_username($operate['add_uid']);
		$this->assign('operate',$operate);
		//获得周数
		if($operate['type']==2){
			//获得节假日
			$holiday=$this->get_holiday($operate['year'],$operate['holiday']);
			$week_detail=$operate['year'].' '.$holiday['name']."（".$holiday['start_date']."至".$holiday['end_date']."）";
						
		}else{
			$week=$this->get_year_month_week(6,$operate['year'],$operate['week']);
			$week_detail=$operate['year']."年 第".$operate['week']."周 （".$week[0]." 至 ".$week[6]."）";
		}
		$this->assign('week_detail',$week_detail);
		//工作汇报详情
		$db_project_operate_detail=M('db_project_operate_detail');
		$db_project_operate_answer=M('db_project_operate_answer');
		$db_project_operate_data=M('db_project_operate_data');
		$where_details['mid']=$_REQUEST['id'];
		$where_details['fid']=32;
		$gzhb_detail=$db_project_operate_detail->where($where_details)->group('sid')->select();
		foreach($gzhb_detail as $key=>$val){
			$gzhb_detail[$key]['second_name']=$this->get_db_universal_label(2,$val['sid']);
			$where_details['sid']=$val['sid'];
			$gzhb_detail[$key]['detail']=$db_project_operate_detail->where($where_details)->select();
			foreach($gzhb_detail[$key]['detail'] as $ka=>$va){
				$where_an['operate_detail_id']=$va['id'];
				$answ=$db_project_operate_answer->where($where_an)->find();
				if(empty($answ)){
					$gzhb_detail[$key]['detail'][$ka]['answer']=1;
				}
				$gzhb_detail[$key]['detail'][$ka]['second_name']=$this->get_db_universal_label(2,$va['sid']);
				$gzhb_detail[$key]['detail'][$ka]['third_name']=$this->get_db_universal_label(3,$va['tid']);
			}
		}
		$this->assign('gzhb_detail',$gzhb_detail);
		//经营数据详情
		if($operate['year_week']<'201804'){
			$where_detail['mid']=$_REQUEST['id'];
			$where_detail['fid']=33;
			$jysj_detail=$db_project_operate_detail->where($where_detail)->group('sid')->select();
			foreach($jysj_detail as $kk=>$vv){
				$where_detail['sid']=$vv['sid'];
				if($vv['sid']==254){
					$mp=$db_project_operate_detail->where($where_detail)->group('tid')->select();
					$mphj['sum']+=count($mp);
					foreach($mp as $k1=>$v1){
						$mp[$k1]['third_name']=$this->get_db_universal_label(3,$v1['tid']);
						$where_detail['tid']=$v1['tid'];
						$type=$db_project_operate_detail->where($where_detail)->order('people desc')->select();
						foreach($type as $k3=>$v3){
							if($v3['people']=='散客'){
								$mp[$k1]['sk_p']=$v3['number'];
								$mp[$k1]['sk_m']=$v3['money']+0;
								$mphj['sk_p']+=$v3['number'];
								$mphj['sk_m']+=$v3['money'];
							}elseif($v3['people']=='团队'){
								$mp[$k1]['td_p']=$v3['number'];
								$mp[$k1]['td_m']=$v3['money']+0;
								$mphj['td_p']+=$v3['number'];
								$mphj['td_m']+=$v3['money'];
							}
							$mp[$k1]['hj_p']+=$v3['number'];
							$mp[$k1]['hj_m']+=$v3['money'];
							$mphj['hj_p']+=$v3['number'];
							$mphj['hj_m']+=$v3['money'];
						}
					}
				}elseif($vv['sid']==255){
					unset($where_detail['tid']);
					$ec=$db_project_operate_detail->where($where_detail)->select();
					$echj['sum']+=count($ec);
					foreach($ec as $k2=>$v2){
						$ec[$k2]['third_name']=$this->get_db_universal_label(3,$v2['tid']);
						$echj['hj']+=$v2['money'];
					}
				}
			}
		}else{
			if($operate['holiday']){
				$holiday=$this->get_holiday($operate['year'],$operate['holiday']);
				$where_detail['date']=array('between',array($holiday['start_date'],$holiday['end_date']));			
			}else{
				$start_end=$this->get_year_month_week(6,$operate['year'],$operate['week']);
				$where_detail['date']=array('between',array($start_end[0],$start_end[6]));
			}
			$where_detail['pid']=$_REQUEST['pid'];
			$where_detail['fid']=33;
			$where_detail['money']=array('exp','is not NULL');
			$jysj_detail=$db_project_operate_data->where($where_detail)->group('sid')->select();
			foreach($jysj_detail as $kk=>$vv){
				$where_detail['sid']=$vv['sid'];
				if($vv['sid']==254){
					$mp=$db_project_operate_data->where($where_detail)->group('tid')->select();
					$mphj['sum']+=count($mp);
					foreach($mp as $k1=>$v1){
						$mp[$k1]['third_name']=$this->get_db_universal_label(3,$v1['tid']);
						$where_detail['tid']=$v1['tid'];
						$type=$db_project_operate_data->field('people,sum(number) as number,sum(money) as money')->where($where_detail)->group('people')->order('people desc')->select();
						foreach($type as $k3=>$v3){
							if($v3['people']=='散客'){
								$mp[$k1]['sk_p']=$v3['number'];
								$mp[$k1]['sk_m']=$v3['money']+0;
								$mphj['sk_p']+=$v3['number'];
								$mphj['sk_m']+=$v3['money'];
							}elseif($v3['people']=='团队'){
								$mp[$k1]['td_p']=$v3['number'];
								$mp[$k1]['td_m']=$v3['money']+0;
								$mphj['td_p']+=$v3['number'];
								$mphj['td_m']+=$v3['money'];
							}
							$mp[$k1]['hj_p']+=$v3['number'];
							$mp[$k1]['hj_m']+=$v3['money'];
							$mphj['hj_p']+=$v3['number'];
							$mphj['hj_m']+=$v3['money'];
						}
					}
				}elseif($vv['sid']==255){
					unset($where_detail['tid']);
					$ec=$db_project_operate_data->field('tid,sum(money) as money')->where($where_detail)->group('tid')->select();
					$echj['sum']+=count($ec);
					foreach($ec as $k2=>$v2){
						$ec[$k2]['third_name']=$this->get_db_universal_label(3,$v2['tid']);
						$echj['hj']+=$v2['money'];
					}
				}
			}
		}
		$this->assign('mp',$mp);
		$this->assign('mphj',$mphj);
		$this->assign('ec',$ec);
		$this->assign('echj',$echj);
		$this->assign('jysj_detail',$jysj_detail);
		$this->display();
	}
	
	//运营周报-->ajax designed by:zhaoshihao 2017-09-25
	function project_operate_audit(){
		if($_REQUEST['action']=='one'){
			//项目详情
			$project=parent::get_project($_REQUEST['pid']);
			$db_project_operate=M('db_project_operate');
			$where['pid']=$_REQUEST['pid'];
			$where['year']=date("Y");
			$where['week']=$_REQUEST['val'];
			if($_REQUEST['mtype']){
				$where['type']=2;
			}else{
				$where['type']=1;
			}
			$have=$db_project_operate->where($where)->find();
			if(empty($have)){
				$data['pid']=$_REQUEST['pid'];
				$data['week']=$_REQUEST['val'];
			}else{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='two')
		{
			//项目详情
			$project=parent::get_project($_REQUEST['pid']);
			$db_project_operate=M('db_project_operate');
			$where['pid']=$_REQUEST['pid'];
			$where['year']=date("Y");
			$holiday=$this->get_holiday(date("Y"),$_REQUEST['week']);
			if($holiday){
				$where['week']=$holiday['week'];
				$where['type']=2;
			}
			else{
				$where['week']=$_REQUEST['week'];
				$where['type']=1;
			}
			$have=$db_project_operate->where($where)->find();
			if(empty($have)){
					if($holiday){
						$where['holiday']=$holiday['name'];
						if(strlen($holiday['week'])==1){$where['year_week']=$holiday['year'].'0'.$holiday['week'];}else{$where['year_week']=$holiday['year'].$holiday['week'];}
					}
					else{
						if(strlen($_REQUEST['week'])==1){$where['year_week']=date("Y").'0'.$_REQUEST['week'];}else{$where['year_week']=date("Y").$_REQUEST['week'];}
					}
				$where['status']=-1;
				$where['add_uid']=$this->login_id;
				$where['add_date']=date('Y-m-d H:i:s');
				$result_bool=$db_project_operate->add($where);
				$db_project_operate_detail=M('db_project_operate_detail');
				$db_project_operate=M('db_project_operate');
				$where['mid']=$result_bool;
				$where['fid']=32;
				$db_project_operate_detail->where($where)->delete();
				foreach($_REQUEST['sid'] as $key=>$val){
					$where['sid']=$val;
					if(!empty($_REQUEST['tid'][$key])){$where['tid']=$_REQUEST['tid'][$key];}else{$where['tid']=NULL;}
					if(!empty($_REQUEST['content'][$key])){$where['content']=$_REQUEST['content'][$key];}else{$where['content']=NULL;}
					$db_project_operate_detail->add($where);
				}
				$data['pid']=$_REQUEST['pid'];
				$data['status']=self::YES;
			}else{
				$data['pid']=$_REQUEST['pid'];
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='edit_two')
		{
			$db_project_operate_detail=M('db_project_operate_detail');
			$db_project_operate=M('db_project_operate');
			$where['mid']=$_REQUEST['mid'];
			$where['fid']=32;
			$db_project_operate_detail->where($where)->delete();
			foreach($_REQUEST['sid'] as $key=>$val){
				$where['sid']=$val;
				if(!empty($_REQUEST['tid'][$key])){$where['tid']=$_REQUEST['tid'][$key];}else{$where['tid']=NULL;}
				if(!empty($_REQUEST['content'][$key])){$where['content']=$_REQUEST['content'][$key];}else{$where['content']=NULL;}
				$db_project_operate_detail->add($where);
			}
			//项目启动表
			$aaa=$db_project_operate->where("id=".$_REQUEST['mid'])->find();
			$data['pid']=$aaa['pid'];
			$data['status']=self::YES;
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='three')
		{
			$db_project_operate=M('db_project_operate');
			$db_project_operate_detail=M('db_project_operate_detail');
			$where['mid']=$_REQUEST['mid'];
			$where['fid']=33;
			$db_project_operate_detail->where($where)->delete();
			foreach($_REQUEST['sid'] as $kk=>$vv){
				$where['sid']=$vv;
				if(!empty($_REQUEST['tid'][$kk])){$where['tid']=$_REQUEST['tid'][$kk];}else{$where['tid']=NULL;}
				if(!empty($_REQUEST['peoples'][$kk])){$where['people']=$_REQUEST['peoples'][$kk];}else{$where['people']=NULL;}
				if(!empty($_REQUEST['numbers'][$kk])){$where['number']=$_REQUEST['numbers'][$kk];}else{$where['number']=NULL;}
				if(!empty($_REQUEST['moneys'][$kk])){$where['money']=$_REQUEST['moneys'][$kk];}else{$where['money']=NULL;}
				$db_project_operate_detail->add($where);
			}
			$aaa=$db_project_operate->where("id=".$_REQUEST['mid'])->find();
			$data['id']=$aaa['pid'];
			$data['status']=self::YES;
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='audit')
		{
			$db_project_operate=M('db_project_operate');
			$where['id']=$_REQUEST['id'];
			$where['status']=1;
			$where['post_uid']=$this->login_id;
			$where['post_date']=date('Y-m-d H:i:s');
			$result_bool=$db_project_operate->save($where);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='remove')
		{
			$db_project_operate_answer=M('db_project_operate_answer');
			$where['mid']=$_REQUEST['id'];
			$result=$db_project_operate_answer->where($where)->find();
			if(!empty($result)){
				$data['status']=self::NO;
				echo json_encode($data);die;
			}else{
				$db_project_operate=M('db_project_operate');
				$where_s['id']=$_REQUEST['id'];
				$where_s['status']=-1;
				$where_s['post_uid']=NULL;
				$where_s['post_date']=NULL;
				$result_bool=$db_project_operate->save($where_s);
				if($result_bool)
				{
					$data['status']=self::YES;
				}
				else
				{
					$data['status']=self::NO;
				}
				echo json_encode($data);die;
			}
		}
		elseif($_REQUEST['action']=='delete')
		{
			$db_project_operate_answer=M('db_project_operate_answer');
			$where['mid']=$_REQUEST['id'];
			$result=$db_project_operate_answer->where($where)->find();
			if(!empty($result)){
				$data['status']=self::NO;
				echo json_encode($data);die;
			}else{
				$db_project_operate=M('db_project_operate');
				$db_project_operate_detail=M('db_project_operate_detail');
				//删除文件
				$where_del['id']=$_REQUEST['id'];
				$result=$db_project_operate->where($where_del)->find();
				$file=str_replace("/dms_erp/uploads", "./uploads", $result['file_url']);
				if (file_exists($file)){
					@unlink($file);
				}
				//删除主表
				$db_project_operate=M('db_project_operate');
				$where_del['id']=$_REQUEST['id'];
				$result_bool=$db_project_operate->where($where_del)->delete();
				//删除副表
				$where_del_plan['mid']=$_REQUEST['id'];
				$db_project_operate_detail->where($where_del_plan)->delete();
				if($result_bool)
				{
					$data['status']=self::YES;
				}
				else
				{
					$data['status']=self::NO;
				}
				echo json_encode($data);die;
			}
		}
	}
	
	//经营数据管理->列表 designed by:zhangsen 2018-01-15
	function project_data_list(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		$this->assign('project_bg',$project_bg);
		if($_REQUEST['year_month']){$time=strtotime($_REQUEST['year_month']);}else{$time=time();}
		$start=date('Y-m-01',$time);
		$end=date('Y-m-d',strtotime("$start +1 month -1 day"));
		$weekarray=array(7,1,2,3,4,5,6);
		$db_project_operate=M('db_project_operate');
		$db_project_operate_answer=M('db_project_operate_answer');
		$db_project_operate_data=M('db_project_operate_data');
		$where['pid']=$_REQUEST['id'];
		$where['fid']=33;
		$where['date']=array(between,array($start,$end));;
		$result=$db_project_operate_data->field('date')->where($where)->order('date desc')->group('date')->select();
		foreach($result as $kk=>$vv){
			$where_a['pid']=$_REQUEST['id'];
			$where_a['fid']=33;
			$where_a['sid']=254;
			$where_a['date']=$vv['date'];
			$operate_m=$db_project_operate_data->field('sum(money) as money,date')->where($where_a)->find();
			$result[$kk]['color']="#f49900";
			$result[$kk]['start_date']=$operate_m['date'];
			$where_b['pid']=$_REQUEST['id'];
			$where_b['fid']=33;
			$where_b['sid']=255;
			$where_b['date']=$vv['date'];
			$operate_b=$db_project_operate_data->field('sum(money) as money,date')->where($where_b)->find();
			$result[$kk]['color']="#4680fe";
			$result[$kk]['start_date']=$operate_b['date'];
			$where_c['pid']=$_REQUEST['id'];
			$where_c['fid']=33;
			$where_c['sid']=496;
			$where_c['date']=$vv['date'];
			$operate_c=$db_project_operate_data->field('sum(money) as money,date')->where($where_c)->find();
			$result[$kk]['color']="#4680fe";
			$result[$kk]['start_date']=$operate_c['date'];
			$result[$kk]['m_money']=$operate_m['money']-0;
			$result[$kk]['e_money']=$operate_b['money']-0;
			$result[$kk]['c_money']=$operate_c['money']-0;
			$result[$kk]['number_wk']=$weekarray[date("w",strtotime($vv['date']))];
			//判断是否可以编辑
			$year=date('Y',strtotime($vv['date']));
			$week=date('W',strtotime($vv['date']))-0;
			$where_m['pid']=$_REQUEST['id'];
			$where_m['type']=1;
			$where_m['year']=$year;
			$where_m['week']=$week;
			$result_p=$db_project_operate->where($where_m)->find();
			if($result_p){
				$where_s['mid']=$result_p['id'];
				$result_d=$db_project_operate_answer->where($where_s)->find();
			}
			if(empty($result_d)){
				$result[$kk]['update']=1;
			}
		}
		$this->assign('pid',$_REQUEST['id']);
		$this->assign('result',$result);
		$this->assign('year_month',date('Y-m',$time));
		$this->display();
	}
	
	//经营数据管理->添加 designed by:zhangsen 2018-01-15
	function project_data_add(){
		$jysj=$this->library_type_list(7,33,NULL,1);
		$i=0;
		foreach($jysj as $key=>$val){
			foreach($val['second'] as $kk=>$vv){
				if(empty($vv['third'])){
					$jysj[$key]['second'][$kk]['num']=$i;
					$i++;
				}else{
					foreach($vv['third'] as $k1=>$v1){
						if(($vv['id']=='254') and ($v1['third_name']=='其它')){$jysj[$key]['second'][$kk]['third'][$k1]['third_name']='其它门票';}
						if(($vv['id']=='255') and ($v1['third_name']=='其它')){$jysj[$key]['second'][$kk]['third'][$k1]['third_name']='其它二消';}
						$jysj[$key]['second'][$kk]['third'][$k1]['num']=$i;
						$i++;
					}
				}
			}
		}
		$this->assign('pid',$_REQUEST['id']);
		$this->assign('date',$_REQUEST['date']);
		$this->assign('jysj',$jysj);
		$this->display();
	}
	
	//经营数据管理->编辑 designed by:zhangsen 2018-01-15
	function project_data_edit(){
		//经营数据
		$db_project_operate_data=M('db_project_operate_data');
		$where_detail['pid']=$_REQUEST['id'];
		$where_detail['fid']=33;
		$where_detail['date']=$_REQUEST['date'];
		$where_detail['money']=array('exp','is not NULL');
		$jysj_detail=$db_project_operate_data->where($where_detail)->order('sid,tid,people desc')->select();
		foreach($jysj_detail as $key=>$val){
			$jysj_detail[$key]['second_name']=$this->get_db_universal_label(2,$val['sid']);
			$jysj_detail[$key]['third_name']=$this->get_db_universal_label(3,$val['tid']);
			$jysj_detail[$key]['money']=$val['money']+0;
			if(empty($val['tid'])){$two[]=$val['sid'];}else{$three[]=$val['tid'];}
		}
		$jysj=$this->library_type_list(7,33,NULL,1);
		$i=0;
		foreach($jysj as $key=>$val){
			foreach($val['second'] as $kk=>$vv){
				if(empty($vv['third'])){
					if(in_array($vv['id'],$two)){
						$jysj[$key]['second'][$kk]['choose']=1;
					}
					$jysj[$key]['second'][$kk]['num']=$i;
					$i++;
				}else{
					foreach($vv['third'] as $k1=>$v1){
						if(($vv['id']=='254') and ($v1['third_name']=='其它')){$jysj[$key]['second'][$kk]['third'][$k1]['third_name']='其它门票';}
						if(($vv['id']=='255') and ($v1['third_name']=='其它')){$jysj[$key]['second'][$kk]['third'][$k1]['third_name']='其它二消';}
						if(in_array($v1['id'],$three)){
							$jysj[$key]['second'][$kk]['third'][$k1]['choose']=1;
						}
						$jysj[$key]['second'][$kk]['third'][$k1]['num']=$i;
						$i++;
					}
				}
			}
		}
		foreach($jysj_detail as $k2=>$v2){
			if(($v2['sid']=='254') and ($v2['third_name']=='其它')){$jysj_detail[$k2]['third_name']='其它门票';}
			if(($v2['sid']=='255') and ($v2['third_name']=='其它')){$jysj_detail[$k2]['third_name']='其它二消';}
			foreach($jysj as $key=>$val){
				foreach($val['second'] as $kk=>$vv){
					if(empty($v2['tid'])){
						if($v2['sid']==$vv['id']){
							$jysj_detail[$k2]['size']=$vv['num'];
						}
					}else{
						foreach($vv['third'] as $k1=>$v1){
							if($v2['tid']==$v1['id']){
								$jysj_detail[$k2]['size']=$v1['num'];
								
							}
						}
					}
				}
			}
		}
		foreach($jysj as $ka=>$va){
			foreach($va['second'] as $kkk=>$vvv){
				foreach($vvv['third'] as $k3=>$v3){
					$ch=0;
					foreach($jysj_detail as $k4=>$v4){
						if($v4['tid']==$v3['id']){
							if($v4['people']=='散客'){$jysj[$ka]['second'][$kkk]['third'][$k3]['sk']='sk';}
							if($v4['people']=='团队'){$jysj[$ka]['second'][$kkk]['third'][$k3]['td']='td';}
							$ch++;
						}
					}
					$jysj[$ka]['second'][$kkk]['third'][$k3]['ch']=$ch;
				}
			}
		}
		$this->assign('jysj_detail',$jysj_detail);
		$this->assign('jysj',$jysj);
		$this->assign('pid',$_REQUEST['id']);
		$this->assign('date',$_REQUEST['date']);
		$this->assign('year_month',$_REQUEST['year_month']);
		$this->display();
	}
	
	//经营数据管理->详情 designed by:zhangsen 2018-01-15
	function project_data_detail(){
		$db_project_operate_data=M('db_project_operate_data');
		$db_project_operate_detail=M('db_project_operate_detail');
		$db_project_operate_answer=M('db_project_operate_answer');
		//经营数据详情
		$where_detail['pid']=$_REQUEST['id'];
		$where_detail['date']=$_REQUEST['date'];
		$where_detail['fid']=33;
		$where_detail['money']=array('exp','is not NULL');
		$jysj_detail=$db_project_operate_data->where($where_detail)->group('sid')->select();//echo $db_project_operate_data->getLastSql();die;
		foreach($jysj_detail as $kk=>$vv){
			$where_detail['sid']=$vv['sid'];
			if($vv['sid']==254){
				$mp=$db_project_operate_data->where($where_detail)->group('tid')->select();
				$mphj['sum']+=count($mp);
				foreach($mp as $k1=>$v1){
					$mp[$k1]['third_name']=$this->get_db_universal_label(3,$v1['tid']);
					$where_detail['tid']=$v1['tid'];
					$type=$db_project_operate_data->where($where_detail)->order('people desc')->select();
					foreach($type as $k3=>$v3){
						if($v3['people']=='散客'){
							$mp[$k1]['sk_p']=$v3['number'];
							$mp[$k1]['sk_m']=$v3['money']+0;
							$mphj['sk_p']+=$v3['number'];
							$mphj['sk_m']+=$v3['money'];
						}elseif($v3['people']=='团队'){
							$mp[$k1]['td_p']=$v3['number'];
							$mp[$k1]['td_m']=$v3['money']+0;
							$mphj['td_p']+=$v3['number'];
							$mphj['td_m']+=$v3['money'];
						}
						$mp[$k1]['hj_p']+=$v3['number'];
						$mp[$k1]['hj_m']+=$v3['money'];
						$mphj['hj_p']+=$v3['number'];
						$mphj['hj_m']+=$v3['money'];
					}
				}
			}elseif($vv['sid']==255){
				unset($where_detail['tid']);
				$ec=$db_project_operate_data->where($where_detail)->select();
				$echj['sum']+=count($ec);
				foreach($ec as $k2=>$v2){
					$ec[$k2]['third_name']=$this->get_db_universal_label(3,$v2['tid']);
					$echj['hj']+=$v2['money'];
				}
			}elseif($vv['sid']==496){
				unset($where_detail['tid']);
				$cb=$db_project_operate_data->where($where_detail)->select();
				$cbhj['sum']+=count($cb);
				foreach($cb as $k2=>$v2){
					$cb[$k2]['third_name']=$this->get_db_universal_label(3,$v2['tid']);
					$cbhj['hj']+=$v2['money'];
				}
			}
		}
		$this->assign('mp',$mp);
		$this->assign('mphj',$mphj);
		$this->assign('ec',$ec);
		$this->assign('echj',$echj);
		$this->assign('cb',$cb);
		$this->assign('cbhj',$cbhj);
		$this->assign('pid',$_REQUEST['id']);
		$this->assign('jysj',$jysj);
		$this->assign('year_month',$_REQUEST['year_month']);
		$this->display();
	}
	
	//经营数据管理->操作 designed by:zhangsen 2018-01-15
	function project_data_list_ajax(){
		if($_REQUEST['action']=='add'){
			$db_project_operate=M('db_project_operate');
			$db_project_operate_data=M('db_project_operate_data');
			$where['date']=$_REQUEST['date'];
			$where['pid']=$_REQUEST['pid'];
			$where['fid']=33;
			$db_project_operate_data->where($where)->delete();
			foreach($_REQUEST['sid'] as $kk=>$vv){
				$where['sid']=$vv;
				if(!empty($_REQUEST['tid'][$kk])){$where['tid']=$_REQUEST['tid'][$kk];}else{$where['tid']=NULL;}
				if(!empty($_REQUEST['peoples'][$kk])){$where['people']=$_REQUEST['peoples'][$kk];}else{$where['people']=NULL;}
				if(!empty($_REQUEST['numbers'][$kk])){$where['number']=$_REQUEST['numbers'][$kk];}else{$where['number']=NULL;}
				if(!empty($_REQUEST['moneys'][$kk])){$where['money']=$_REQUEST['moneys'][$kk];}else{$where['money']=NULL;}
				$db_project_operate_data->add($where);
			}
			$data['status']=self::YES;
			$data['pid']=$_REQUEST['pid'];
			$data['year_month']=$_REQUEST['year_month'];
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='delete'){
			$db_project_operate_data=M('db_project_operate_data');
			$where_s['pid']=$_REQUEST['pid'];
			$where_s['date']=$_REQUEST['date'];
			$db_project_operate_data->where($where_s)->delete();
			$data['status']=self::YES;
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='dates'){
			$db_project_operate=M('db_project_operate');
			$db_project_operate_answer=M('db_project_operate_answer');
			$data['status']=1;
			$data['date']=$_REQUEST['date'];
			if($_REQUEST['pid']==-10){
				$data['status']=0;
				$data['msg']="请先选择项目";
				echo json_encode($data);die;
			}else{
				$db_project_operate_data=M('db_project_operate_data');
				$where['pid']=$_REQUEST['pid'];
				$where['date']=$_REQUEST['date'];
				$result=$db_project_operate_data->where($where)->find();
				if($result){
					$data['status']=0;
					$data['msg']="当天已填报经营数据";
					echo json_encode($data);die;
				}
			}
			//判断日期是否小于当前日期30天内
			$second1=strtotime($_REQUEST['date']);
			$second2=strtotime(date('Y-m-d'));
			if($second1>$second2){
				$nodate=1;
			}
			if(($second2-$second1)/86400>30){
				$nodate=1;
			}
			if($nodate==1){
				$data['status']=0;
				$data['msg']="只能填报30天前的经营数据";
				echo json_encode($data);die;
			}
			//判断是否可以编辑
			$year=date('Y',strtotime($_REQUEST['date']));
			$week=date('W',strtotime($_REQUEST['date']))-0;
			$where_p['pid']=$_REQUEST['pid'];
			$where_p['type']=1;
			$where_p['year']=$year;
			$where_p['week']=$week;
			$result_p=$db_project_operate->where($where_p)->find();
			if($result_p){
				$where_a['mid']=$result_p['id'];
				$result_a=$db_project_operate_answer->where($where_a)->find();
			}
			if($result_a){
				$data['status']=0;
				$data['msg']="该周周报已批复，不能添加经营数据";
				echo json_encode($data);die;
			}
			$date_start_end=$this->get_year_month_week(6,substr("201804",0,4),substr("201804",5,1));
			if($_REQUEST['date']<$date_start_end[0]){
				$data['status']=0;
				$data['msg']="不能填报".$date_start_end[0]."之前的经营数据";
				echo json_encode($data);die;
			}
			echo json_encode($data);die;
		}
	}
	
	//项目功能-->项目概况 designed by:zhangsen 2017-11-30
	function project_situation(){
		//项目详情
		$project=$this->get_project($_REQUEST['id']);
		$type_list=$this->get_db_universal_type(3,1,NULL,NULL,NULL,$project['center_id']);
		$project['center_name']=$type_list['name1'];
		if(!empty($project['status'])){
			$result_type=$this->get_db_universal_type(4,1,NULL,null,NULL,$project['status']);
			$project['status_name']=$result_type['name1'];
		}
		$this->assign('project',$project);
		$this->assign('project_bg',$this->get_project_bg($_REQUEST['id']));
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}
	
	//项目功能-->项目启动表 designed by:zhangsen 2017-11-30
	function project_bg(){
		//项目详情
		$project=$this->get_project($_REQUEST['id']);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		if($project['center_id']==130005 and empty($project['info_id'])){
			$db_project_user=M('db_project_user');
			$where['pid']=$project['id'];
			$result=$db_project_user->where($where)->order('come_date desc')->group('uid')->select();
			foreach($result as $key=>$value){
				$user_str.=$this->get_username($value['uid']).',';
			}
			$project_bg['project_users']=rtrim($user_str,',');
		}
		$project_bg['business_type_name']=$this->get_db_universal_label(2,$project_bg['business_type']);
		$project_bg['p_type_name']=$this->get_db_universal_label(2,$project_bg['p_type']);
		$bus=$this->get_project($project_bg['bus_id']);
		$project_bg['bus_num']=$bus['project_num'];
		$project_bg['edit_user']=$this->get_username($project_bg['edit_uid']);
		$project_bg['add_user']=$this->get_username($project_bg['add_uid']);
		$investigate_sum=count(explode(',',$project_bg['investigate_date']))-1;
		$project_bg['investigate_dates']=explode(',',$project_bg['investigate_date']);
		$investigate_dates=explode(',',$project_bg['investigate_date']);
		$this->assign("investigate_dates",$investigate_dates);
		$this->assign("investigate_sum",$investigate_sum);
		//选择用户下拉菜单
		$db_project_negotiation=M('db_project_negotiation');
		$where_n['pid']=$_REQUEST['id'];
		$negotiation=$db_project_negotiation->where($where_n)->select();
		$buid=array();
		foreach($negotiation as $k1=>$v1){
			if(!empty($v1['type_uid'])){$buid[]=$v1['type_uid'];}
			if(!empty($v1['bus_uids'])){
				foreach(explode(',',$v1['bus_uids']) as $k2=>$v2){
					$buid[]=$v2;
				}
			}
		}
		$user_list=$this->get_all_department();
		foreach($user_list as $key=>$val){
			foreach($val['user'] as $ke=>$va){
				foreach($buid as $kk=>$vv){
					if($vv==$va['id']){
						$user_list[$key]['user'][$ke]['cho']=1;
					}
				}
			}
			if(!empty($project['info_id'])){
				if($val['center_id']!=130003){
					unset($user_list[$key]);
				}
			}
		}
		$this->assign("user_list",$user_list);
		//项目客户(关联拓展项目的运营项目客户=运营客户+拓展客户)
		$db_project_contacts=M('db_project_contacts');
		if(!empty($project_bg['bus_id'])){
			$project_c=$this->get_project($project_bg['bus_id']);
			if($project['center_id']==130005 or $project['center_id']==130011 and empty($project['info_id'])){
				$project_bg_c=$this->get_project_bg($project_bg['bus_id']);
			}
			$cids_c=explode(',',$project_c['cid']);
			foreach($cids_c as $kc=>$vc){
				$info_c['id']=$vc;
				$customers_c[$kc]=$db_project_contacts->where($info_c)->find();
				$customers_c[$kc]['province']=$this->get_province_city($customers_c[$kc]['province_id'],2);
				$customers_c[$kc]['city']=$this->get_province_city($customers_c[$kc]['city_id'],2);
			}
			$this->assign("project_c",$project_c);
			$this->assign("customer_c",$customers_c);
			$this->assign("project_bg_c",$project_bg_c);
		}
		if(!empty($project['cid'])){
			$cids=explode(',',$project['cid']);
			foreach($cids as $kc=>$vc){
				$info_where['id']=$vc;
				$customers_results[$kc]=$db_project_contacts->where($info_where)->find();
				$customers_results[$kc]['province']=$this->get_province_city($customers_results[$kc]['province_id'],2);
				$customers_results[$kc]['city']=$this->get_province_city($customers_results[$kc]['city_id'],2);
			}
		}
		$this->assign("customer",$customers_results);
		//项目客户下拉菜单
		$where_all['center_id']=$project['center_id'];
		$customers_result=$db_project_contacts->where($where_all)->order('username')->select();
		$Firstletter = new \Think\Firstletter();
		foreach($customers_result as $key=>$value)
		{
			if(!empty($value['province_id'])){$customers_result[$key]['province']=$this->get_province_city($value['province_id'],2);}
			if(!empty($value['city_id'])){$customers_result[$key]['city']=$this->get_province_city($value['city_id'],2);}
			$customers_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($customers_result as $key=>$value){
			$customers[$value['first_letter']][]=$value;
		}
		ksort($customers);
		$this->assign("customers",json_encode($customers));
		//项目分类下拉菜单
		$ptype_list=$this->library_type_list(6,22);
		$this->assign('ptype_list',$ptype_list[0]['second']);
		//商务项目号下拉菜单
		$db_project=M('db_project');
		$where_b['info_id']=array('exp','IS not NULL');
		$where_b['db_project_bg.status']=1;
		$business=$db_project->field('db_project.*')->join('left join db_project_bg on db_project.id=db_project_bg.pid')->where($where_b)->order('db_project.add_date desc')->select();
		$this->assign('business',$business);
		//商务类型下拉菜单
		$bus_list=$this->library_type_list(6,23);
		$this->assign('bus_list',$bus_list[0]['second']);
		$this->assign('project',$project);
		$this->assign('project_bg',$project_bg);
		//判断经营项目项目阶段能否编辑
		$this->assign('opera',is_operat_stage_update($_REQUEST['id']));
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}
	
	//项目功能-->项目启动表->根据拓展项目号获取数据 designed by:zhangsen 2017-10-25
	function project_bg_ajax(){
		//项目详情
		$project=$this->get_project($_REQUEST['id']);
		//项目客户
		$db_project_contacts=M('db_project_contacts');
		if(!empty($project['cid'])){
			$cids=explode(',',$project['cid']);
			foreach($cids as $kc=>$vc){
				$info_where['id']=$vc;
				$customers_results[$kc]=$db_project_contacts->where($info_where)->find();
				$customers_results[$kc]['province']=$this->get_province_city($customers_results[$kc]['province_id'],2);
				$customers_results[$kc]['city']=$this->get_province_city($customers_results[$kc]['city_id'],2);
			}
		}
		//项目启动表
		$data=$this->get_project_bg($_REQUEST['id']);
		$data['person']=$customers_results;
		echo json_encode($data);die;
	}
	
	//项目功能-->项目启动表-->操作 designed by:zhangsen 2017-11-30
	function project_bg_submit(){
		$db_project_bg=M('db_project_bg');
		$db_project=M('db_project');
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['pid']);
		//建设，运营
		//保存客户
		sort($_REQUEST['hide_val']);
		$where_pr['id']=$_REQUEST['pid'];
		$where_pr['cid']=join(',',array_unique($_REQUEST['hide_val']));
		$where_pr['cid_uid']=$this->login_id;
		$where_pr['cid_date']=date('Y-m-d H:i:s');
		$db_project->save($where_pr);
		$where['pid']=$_REQUEST['pid'];
		if(!empty($_REQUEST['sname']))
		{
			//新增项目分类
			$db_universal_label2=M('db_universal_label2');
			$where_center['first_id']=22;
			$where_center['second_name']=$_REQUEST['sname'];
			$exist_center=$db_universal_label2->where($where_center)->find();
			if(!empty($exist_center))
			{
				$data['status']=self::NO;
				$data['msg']='该项目分类已存在！';
				echo json_encode($data);die;
			}
			$where_center_sort['first_id']=$where_center['first_id'];
			$where_center['first_sort']=$db_universal_label2->where($where_center_sort)->order('second_sort desc')->getField('second_sort')+1;
			$where_center['add_uid']=$this->login_id;
			$where_center['add_date']=date("Y-m-d H:i:s");
			$p_type_id=$db_universal_label2->add($where_center);
			$where['p_type']=$p_type_id;
		}else{
			$where['p_type']=$_REQUEST['sid']?$_REQUEST['sid']:NULL;
		}
		if(!empty($_REQUEST['bus_id']) and ($_REQUEST['bus_id']!=-10)){$where['bus_id']=$_REQUEST['bus_id'];}else{$where['bus_id']=NULL;}
		if(!empty($_REQUEST['contract_money'])){$where['contract_money']=$_REQUEST['contract_money'];}else{$where['contract_money']=NULL;}
		if(!empty($_REQUEST['execute_money'])){$where['execute_money']=$_REQUEST['execute_money'];}else{$where['execute_money']=NULL;}
		if(!empty($_REQUEST['cycle_date'])){$where['cycle_date']=$_REQUEST['cycle_date'];}else{$where['cycle_date']=NULL;}
		if(!empty($_REQUEST['project_budget'])){$where['project_budget']=$_REQUEST['project_budget'];}else{$where['project_budget']=NULL;}
		//建设，运营
		//运营
		if(!empty($_REQUEST['cooperation_mode'])){$where['cooperation_mode']=$_REQUEST['cooperation_mode'];}else{$where['cooperation_mode']=NULL;}
		if(!empty($_REQUEST['scenic_level'])){$where['scenic_level']=$_REQUEST['scenic_level'];}else{$where['scenic_level']=NULL;}
		if(!empty($_REQUEST['project_stage'])){$where['project_stage']=$_REQUEST['project_stage'];}
		//运营
		//建设
		$investigate_date=array_filter($_REQUEST['investigate_date']); 
		$investigate_date=array_unique($investigate_date);
		sort($investigate_date);
		if(!empty($_REQUEST['investigate_date'])){$where['investigate_date']=join(',',$investigate_date);}else{$where['investigate_date']=NULL;}
		//建设
		//商务
		if(!empty($_REQUEST['fname']))
		{
			//新增商务类型
			$db_universal_label2=M('db_universal_label2');
			$where_bus['first_id']=23;
			$where_bus['second_name']=$_REQUEST['fname'];
			$exist_center=$db_universal_label2->where($where_bus)->find();
			if(!empty($exist_center))
			{
				$data['status']=self::NO;
				$data['msg']='该商务类型已存在！';
				echo json_encode($data);die;
			}
			$where_bus_sort['first_id']=$where_bus['first_id'];
			$where_bus['first_sort']=$db_universal_label2->where($where_bus_sort)->order('second_sort desc')->getField('second_sort')+1;
			$where_bus['add_uid']=$this->login_id;
			$where_bus['add_date']=date("Y-m-d H:i:s");
			$business_type_id=$db_universal_label2->add($where_bus);
			$where['business_type']=$business_type_id;
		}else{
			$where['business_type']=$_REQUEST['fid']?$_REQUEST['fid']:NULL;
		}
		if(!empty($_REQUEST['channel'])){$where['channel']=$_REQUEST['channel'];}else{$where['channel']=NULL;}
		//商务
		$where['project_uids']=$_REQUEST['tid'];
		$where['project_users']=$_REQUEST['tname'];
		$where['project_introduce']=$_REQUEST['project_introduce'];
		if(!empty($_REQUEST['party_background'])){$where['party_background']=$_REQUEST['party_background'];}else{$where['party_background']=NULL;}
		if(!empty($_REQUEST['cooperative_appeals'])){$where['cooperative_appeals']=$_REQUEST['cooperative_appeals'];}else{$where['cooperative_appeals']=NULL;}
		if($_REQUEST['action']=='cancel'){
			$where_c['id']=$_REQUEST['id'];
			$where_c['status']=-1;
			$db_project_bg->save($where_c);
		}else{
			if(empty($project_bg)){
				$where['add_uid']=$this->login_id;
				$where['add_date']=date('Y-m-d H:i:s');
				$where['edit_uid']=$this->login_id;
				$where['edit_date']=date('Y-m-d H:i:s');
				$where['status']=1;
				$db_project_bg->add($where);
			}else{
				$where['id']=$_REQUEST['id'];
				$where['edit_uid']=$this->login_id;
				$where['edit_date']=date('Y-m-d H:i:s');
				$where['status']=1;
				$db_project_bg->save($where);
			}
		}
		$data['status']=self::YES;
		$data['msg']='操作成功';
		echo json_encode($data);die;
	}
	
	//项目功能-->前期沟通 designed by:zhangsen 2017-11-21
	function project_communicate(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		$this->assign('project_bg',$project_bg);
		//列表
		$db_project_communicate=M('db_project_communicate');
		$where['pid']=$_REQUEST['id'];
		$result=$db_project_communicate->where($where)->order('date desc')->select();
		foreach($result as $key=>$value){
			if(!empty($value['remark_text'])){
				$arr=explode(',',$value['remark']);
				foreach($arr as $kk=>$vv){
					if($vv=='其它'){
						$arr[$kk]=$vv.'('.$value['remark_text'].')';
					}
				}
				$result[$key]['remark']=join(',',$arr);
			}
		}
		$this->assign('result',$result);
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}
	
	//项目功能-->前期沟通-->添加 designed by:zhangsen 2017-11-21
	function project_communicate_add(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		$this->display();
	}
	
	//项目功能-->商务谈判 designed by:zhangsen 2017-11-21
	function project_negotiation(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		$this->assign('project_bg',$project_bg);
		//列表
		$db_project_negotiation=M('db_project_negotiation');
		$where['pid']=$_REQUEST['id'];
		$result=$db_project_negotiation->where($where)->order('date desc')->select();
		$db_project_contacts=M('db_project_contacts');
		foreach($result as $key=>$value){
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			$user=array();
			if(!empty($value['type_remark']) or !empty($value['type_uid'])){
				$arr=explode(',',$value['type']);
				foreach($arr as $kk=>$vv){
					if($vv=='其它'){
						$arr[$kk]=$vv.'('.$value['type_remark'].')';
					}
					if($vv=='工作建议书'){
						$arr[$kk]=$vv.'('.$this->get_username($value['type_uid']).')';
					}
				}
				$result[$key]['type']=join(',',$arr);
			}
			foreach(explode(',',$value['bus_uids']) as $k1=>$v1){
				$user[]=$this->get_username($v1);
			}
			$result[$key]['bus_users']=join(',',$user);
			$where_c['id']=$value['ocid'];
			$cids=$db_project_contacts->where($where_c)->find();
			$result[$key]['ouser']=$cids['username'];
			if(!empty($cids['company_name'])){$result[$key]['company_name']=$cids['company_name'];}
		}
		$this->assign('result',$result);
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}

	//项目功能-->商务谈判-->添加 designed by:zhangsen 2017-11-21
	function project_negotiation_add(){
		$Firstletter = new \Think\Firstletter();
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id'],1);
		$this->assign('project_bg',$project_bg);
		//项目客户下拉菜单
		$db_project_contacts=M('db_project_contacts');
		$where_all['center_id']=$project['center_id'];
		$customers_result=$db_project_contacts->where($where_all)->order('username')->select();
		foreach($customers_result as $key=>$value)
		{
			if(!empty($value['province_id'])){$customers_result[$key]['province']=$this->get_province_city($value['province_id'],2);}
			if(!empty($value['city_id'])){$customers_result[$key]['city']=$this->get_province_city($value['city_id'],2);}
			$customers_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($customers_result as $key=>$value){
			$customers[$value['first_letter']][]=$value;
		}
		ksort($customers);
		//姓名下拉菜单
		$use=array();
		$use[]=$project['manager_uid'];
		$use[]=$project['director_uid'];
		foreach(explode(',',$project_bg['project_uids']) as $ka=>$va){
			$use[]=$va;
		}
		$use=array_flip(array_flip($use));
		$use=join(',',$use);
		$this->assign('result1',$this->get_all_username(1,NULL,NULL,NULL,$use));
		//选择用户下拉菜单
		$this->assign("user_list",$this->get_all_department($use));
		$this->assign("customers",json_encode($customers));
		$this->display();
	}
	
	//项目功能-->商务签约 designed by:zhangsen 2017-11-21
	function project_contract(){
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		if($project['file_formal_url']){
			$file_url=explode(',',$project['file_formal_url']);
			$file_name=explode(',',$project['file_formal_name']);
			$project['file_formal_url']=$file_url;
			$project['file_formal_name']=$file_name;	
		}
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['id']);
		$this->assign('project_bg',$project_bg);
		$db_project_contacts=M('db_project_contacts');
		if(!empty($project['cid'])){
			$cids=explode(',',$project['cid']);
			foreach($cids as $kc=>$vc){
				$info_where['id']=$vc;
				$customers_results[$kc]=$db_project_contacts->where($info_where)->find();
				$customers_results[$kc]['province']=$this->get_province_city($customers_results[$kc]['province_id'],2);
				$customers_results[$kc]['city']=$this->get_province_city($customers_results[$kc]['city_id'],2);
			}
		}
		$this->assign("customer",$customers_results);
		if(empty($project['info_id'])){$this->assign('qs','XM');$this->assign('manage_id',$this->login_manage_id);}else{$this->assign('qs','SW');$this->assign('manage_id',$this->login_sw_manage_id);}
		$this->display();
	}
	
	//项目功能-->三阶段-->ajax designed by:zhangsen 2017-11-21
	function stage_ajax(){
		if($_REQUEST['action']=='communicate_add'){
			$db_project_communicate=M('db_project_communicate');
			$where['pid']=$_REQUEST['pid'];
			$where['date']=$_REQUEST['data'];
			$where['work_type']=$_REQUEST['work_type'];
			if(!empty($_REQUEST['remark'])){
				if(!empty($_REQUEST['other'])){
					$where['remark']=join(',',$_REQUEST['remark']).',其它';
					$where['remark_text']=$_REQUEST['other'];
				}else{
					$where['remark']=join(',',$_REQUEST['remark']);
				}
			}else{
				if(!empty($_REQUEST['other'])){
					$where['remark']='其它';
					$where['remark_text']=$_REQUEST['other'];
				}
			}
			if(!empty($_REQUEST['file_url'])){$where['file_url']=$_REQUEST['file_url'];}
			if(!empty($_REQUEST['file_name'])){$where['file_name']=$_REQUEST['file_name'];}
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_project_communicate->add($where);
		}elseif($_REQUEST['action']=='communicate_delete'){
			$db_project_communicate=M('db_project_communicate');
			$where['id']=$_REQUEST['id'];
			$result_d=$db_project_communicate->where($where)->find();
			//删除附件
			$url=$result_d['file_url'];
			$file=str_replace("/dms_erp/uploads", "./uploads",$url);
			if (file_exists($file)) {
				@unlink($file);
			}
			$result_bool=$db_project_communicate->where($where)->delete();
		}elseif($_REQUEST['action']=='negotiation_add'){
			$db_project_negotiation=M('db_project_negotiation');
			$where['pid']=$_REQUEST['pid'];
			$where['date']=$_REQUEST['data'];
			$where['type']=join(',',$_REQUEST['type']);
			$type_str=join(',',$_REQUEST['type']);
			if(!empty($_REQUEST['tid'])){
				$where['type_uid']=$_REQUEST['tid'];
				$type_str=ltrim($type_str.',工作建议书',',');
			}
			if(!empty($_REQUEST['remark_text'])){
				$where['type_remark']=$_REQUEST['remark_text'];
				$type_str=ltrim($type_str.',其它',',');
			}
			$where['type']=$type_str;
			if(!empty($_REQUEST['cid'])){$where['bus_uids']=$_REQUEST['cid'];}
			$where['ocid']=$_REQUEST['mid'];
			$where['content']=$_REQUEST['content'];
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_project_negotiation->add($where);
			$data['url']='';
		}elseif($_REQUEST['action']=='negotiation_delete'){
			$db_project_negotiation=M('db_project_negotiation');
			$where['id']=$_REQUEST['id'];
			$result_d=$db_project_negotiation->where($where)->find();
			//删除附件
			$url=$result_d['file_url'];
			$file=str_replace("/dms_erp/uploads", "./uploads",$url);
			if (file_exists($file)) {
				@unlink($file);
			}
			$result_bool=$db_project_negotiation->where($where)->delete();
		}elseif($_REQUEST['action']=='gt_status'){
			$db_project=M('db_project');
			$project=parent::get_project($_REQUEST['id']);
			if($project['tp_status']==1 or $project['qy_status']==1){
				if($project['gt_status']==$_REQUEST['status']){}
			}else{
				$where['id']=$_REQUEST['id'];
				$where['gt_status']=$_REQUEST['status'];
				$result_bool=$db_project->save($where);
			}
		}elseif($_REQUEST['action']=='tp_status'){
			$db_project=M('db_project');
			$project=parent::get_project($_REQUEST['id']);
			if($project['qy_status']==1 or $project['gt_status']==-1 or $project['gt_status']==NULL){
				if($project['tp_status']==$_REQUEST['status']){}
			}else{
				$where['id']=$_REQUEST['id'];
				$where['tp_status']=$_REQUEST['status'];
				$result_bool=$db_project->save($where);
			}
		}
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}

	//任务管理 designed by:zhangsen 2017-12-28
	function task_list(){
		//项目详情
		$project=parent::get_project($_REQUEST['pid']);
		$this->assign('project',$project);
		//项目启动表
		$project_bg=$this->get_project_bg($_REQUEST['pid']);
		$this->assign('project_bg',$project_bg);
		//查找
		$type1=I('type1',-10);
		$this->assign('type1',$type1);
		$type2=I('type2',-10);
		$this->assign('type2',$type2);
		//分类筛选
		$type1_list=$this->library_type_list(18,NULL,NULL,1);
		$this->assign('type1_list',$type1_list);
		foreach($type1_list as $key=>$value){
			if($type1==$value['id']){
				$type2_list=$value['second'];
				$this->assign('type2_list',$type2_list);
			}
		}
		$db_task=M('db_task');
		$db_task_detail=M('db_task_detail');
		$where['db_audit_main.type']=5;
		if($_REQUEST['pid']){
			$where['db_audit_main.pid']=$_REQUEST['pid'];
			$where['db_task.work_type']=2;
		}else{
			$where_d['uid']=$this->login_id;
			$result_d=$db_task_detail->field('mid')->where($where_d)->select();
			foreach($result_d as $key=>$value){
				$mid_str.=$value['mid'].',';
			}
			if($mid_str){
				$where['_string']="db_task.id in (".rtrim($mid_str,',').") or db_task.head_uid='".$this->login_id."' or db_audit_main.add_uid='".$this->login_id."'";
			}
			else{
				$where['_string']=" db_task.head_uid='".$this->login_id."' or db_audit_main.add_uid='".$this->login_id."'";
			}
		}
		if($type1!=-10){$where['type1']=$type1;}
		if($type2!=-10){$where['type2']=$type2;}
		$count=$db_task->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->count();
		$Page = new \Think\Page($count,15);
		$show=$Page->show();
		$result=$db_task->field('db_audit_main.order_number,db_audit_main.status,db_audit_main.add_uid,db_audit_main.add_date,db_audit_main.id as tid,db_task.*')->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->order('add_date desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($result as $key=>$value){
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			$result[$key]['head_user']=$this->get_username($value['head_uid']);
			$where_dm['mid']=$value['id'];
			$result_dm=$db_task_detail->where($where_dm)->select();
			$ct=0;
			foreach($result_dm as $k=>$v){
				if($v['status']==-1){$ct++;}
			}
			$result[$key]['ct']=$ct;
			$result[$key]['result_d']=$result_dm;
		}
		$this->assign('pid',$_REQUEST['pid']);
		$this->assign('page',$show);
		$this->assign('result',$result);
		$this->display();
	}
	
	//任务管理-->添加人员安排 designed by:zhangsen 2017-12-28
	function task_add_pep(){
		$this->assign('id',$_REQUEST['id']);
		$this->assign('pid',$_REQUEST['pid']);
		$db_task=M('db_task');
		$where['db_task.id']=$_REQUEST['id'];
		$where['db_audit_main.type']=5;
		$result=$db_task->field('db_task.*,db_audit_main.*,db_task.id as tid')->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->find();
		$this->assign('result',$result);
		$db_project_user=M('db_project_user');
		$where_p['pid']=$result['pid'];
		$where_p['db_user.status']=1;
		$user_result=$db_project_user->field('db_project_user.*,db_user.username')->join('db_user on db_project_user.uid=db_user.id')->where($where_p)->order('spell desc')->group('uid')->select();
		foreach($user_result as $key=>$value){
			$user_result[$key]['username']=$value['username'];
			$user_result[$key]['id']=$value['uid'];
		}
		$Firstletter = new \Think\Firstletter();
		foreach($user_result as $key=>$value)
		{
			$user_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($user_result as $key=>$value){
			$user_list[$value['first_letter']][]=$value;
		}
		ksort($user_list);
		$this->assign('user_list',json_encode($user_list));
		$this->assign('ppid',$_REQUEST['ppid']);
		$this->display();
	}
	
	//任务管理-->添加任务 designed by:zhangsen 2017-12-28
	function task_add_task(){
		//类别1筛选
		$type1_list=$this->library_type_list(18,NULL,NULL,1);
		$this->assign('type1_list',$type1_list);
		$this->assign('pid',$_REQUEST['pid']);
		//项目详情
		$project=parent::get_project($_REQUEST['pid']);
		$this->assign('project',$project);
		$this->assign('project_bg',$this->get_project_bg($_REQUEST['pid']));
		//人员下拉菜单
		$user_result=$this->get_all_username();
		$Firstletter = new \Think\Firstletter();
		foreach($user_result as $key=>$value)
		{
			$user_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($user_result as $key=>$value){
			$user_list[$value['first_letter']][]=$value;
		}
		ksort($user_list);
		$this->assign('user_list',json_encode($user_list));
		$this->display();
	}
	
	//任务管理-->编辑任务 designed by:zhangsen 2017-12-28
	function task_edit_task(){
		$db_task=M('db_task');
		$where_t['id']=$_REQUEST['id'];
		$result=$db_task->where($where_t)->find();
		$type1_list=$this->library_type_list(18,NULL,NULL,1);
		$this->assign('type1_list',$type1_list);
		foreach($type1_list as $key=>$value){
			if($result['type1']==$value['id']){
				$type2_list=$value['second'];
				$this->assign('type2_list',$type2_list);
				foreach($type2_list as $kk=>$vv){
					if($result['type2']==$vv['id']){
						$type3_list=$vv['third'];
						$this->assign('type3_list',$type3_list);
					}
				}
			}
		}
		$result['head_user']=$this->get_username($result['head_uid']);
		$result['start_date']=strtr($result['start_date'],'-','/');
		$result['end_date']=strtr($result['end_date'],'-','/');
		$this->assign('result',$result);
		$this->assign('pid',$_REQUEST['pid']);
		$this->assign('id',$_REQUEST['id']);
		//项目详情
		$project=parent::get_project($_REQUEST['pid']);
		$this->assign('project',$project);
		$this->assign('project_bg',$this->get_project_bg($_REQUEST['pid']));
		//人员下拉菜单
		$user_result=$this->get_all_username();
		$Firstletter = new \Think\Firstletter();
		foreach($user_result as $key=>$value)
		{
			$user_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($user_result as $key=>$value){
			$user_list[$value['first_letter']][]=$value;
		}
		ksort($user_list);
		$this->assign('user_list',json_encode($user_list));
		$this->display();
	}
	
	//任务管理-->编辑人员安排 designed by:zhangsen 2017-12-28
	function task_edit_pep(){
		$this->assign('mid',$_REQUEST['mid']);
		$this->assign('id',$_REQUEST['id']);
		$this->assign('pid',$_REQUEST['pid']);
		$db_task=M('db_task');
		$db_task_detail=M('db_task_detail');
		$where['db_task.id']=$_REQUEST['id'];
		$where['db_audit_main.type']=5;
		$result=$db_task->field('db_task.*,db_audit_main.*,db_task.id as tid')->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->find();
		$this->assign('result',$result);
		$where_d['id']=$_REQUEST['mid'];
		$detail=$db_task_detail->where($where_d)->find();
		$detail['name']=$this->get_username($detail['uid']);
		$detail['end_date']=strtr($detail['end_date'],'-','/');
		$this->assign('detail',$detail);
		$db_project_user=M('db_project_user');
		$where_p['pid']=$result['pid'];
		$where_p['db_user.status']=1;
		$user_result=$db_project_user->field('db_project_user.*,db_user.username')->join('db_user on db_project_user.uid=db_user.id')->where($where_p)->order('spell desc')->group('uid')->select();
		foreach($user_result as $key=>$value){
			$user_result[$key]['username']=$value['username'];
			$user_result[$key]['id']=$value['uid'];
		}
		$Firstletter = new \Think\Firstletter();
		foreach($user_result as $key=>$value)
		{
			$user_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($user_result as $key=>$value){
			$user_list[$value['first_letter']][]=$value;
		}
		ksort($user_list);
		$this->assign('user_list',json_encode($user_list));
		$this->assign('ppid',$_REQUEST['ppid']);
		$this->display();
	}
	
	//任务管理-->详情 designed by:zhangsen 2017-12-28
	function task_detail(){
		$db_task=M('db_task');
		$where['db_task.id']=$_REQUEST['id'];
		$where['db_audit_main.type']=5;
		$result=$db_task->field('db_task.*,db_audit_main.*,db_task.id as tid')->join('db_audit_main on db_task.id=db_audit_main.audit_id')->where($where)->find();
		$result['add_user']=$this->get_username($result['add_uid']);
		if($result['file_url']){$file_url_str=explode(',',$result['file_url']);}
		if($result['file_name']){$file_name_str=explode(',',$result['file_name']);}
		$result['file_url']=$file_url_str;
		$result['file_name']=$file_name_str;
		if($result['file_url_c']){$file_url_c_str=explode(',',$result['file_url_c']);}
		if($result['file_name_c']){$file_name_c_str=explode(',',$result['file_name_c']);}
		$result['file_url_c']=$file_url_c_str;
		$result['file_name_c']=$file_name_c_str;
		$project=$this->get_project($result['pid']);
		$result['project_num']=$project['project_num'];
		$result['project_name']=$project['project_name'];
		$result['dh_user']=$this->get_username($result['dh_uid']);
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		//任务分类
		$where_t1['id']=$result['type1'];
		$name_t1=$db_universal_label1->where($where_t1)->find();
		$result['type1_name']=$name_t1['first_name'];
		//任务子类
		$where_t2['id']=$result['type2'];
		$name_t2=$db_universal_label2->where($where_t2)->find();
		$result['type2_name']=$name_t2['second_name'];
		//任务子类
		$where_t3['id']=$result['type3'];
		$name_t3=$db_universal_label3->where($where_t3)->find();
		$result['type3_name']=$name_t3['third_name'];
		$result['head_user']=$this->get_username($result['head_uid']);
		////
		//审核人员
		$audit_arr=$this->audit_user(5,$result['audit_type']);
		foreach($audit_arr as $kk=>$vv){
			if($result['status']>$vv['audit_sort']){
				switch($vv['audit_name']){
					case '审核人':
						$audit_arr[$kk]['audit_user']=$this->get_username($result['audit1_uid']);
						$audit_arr[$kk]['audit_date']=$result['audit1_date'];
						$audit_arr[$kk]['audit_comments']=$result['audit1_message'];
					break;
					case '审批人':
						$audit_arr[$kk]['audit_user']=$this->get_username($result['audit2_uid']);
						$audit_arr[$kk]['audit_date']=$result['audit2_date'];
						$audit_arr[$kk]['audit_comments']=$result['audit2_message'];
					break;
				}
			}
			if(empty($vv['audit_uid']) and empty($audit_arr[$kk]['audit_user'])){unset($audit_arr[$kk]);}
		}
		$array2=array("audit_name"=>"申请人","audit_user"=>"".$this->get_username($result['post_uid'])."","audit_date"=>"".$result['post_date']."","audit_sort"=>"-1");
		array_unshift($audit_arr,$array2);
		foreach($audit_arr as $k1=>$v1){
			if($result['arrive_status']==$v1['audit_sort']){
				$arrive_name=$v1['audit_name'];
				if($result['arrive_status']=='-1'){$arrive_info=$result['submit_info'];}else{$arrive_info=$v1['audit_comments'];}
				$arrive_date=$v1['audit_date'];
			}
			if($result['dh_status']==$v1['audit_sort']){
				$audit_arr[$k1]['arrive_name']=$arrive_name;
				$audit_arr[$k1]['dh_message']=$result['dh_message'];
				$audit_arr[$k1]['dh_date']=$result['dh_date'];
				$audit_arr[$k1]['arrive_info']=$arrive_info;
				$audit_arr[$k1]['arrive_date']=$arrive_date;
			}
		}
		$this->assign('audit_user',$audit_arr);
		foreach($audit_arr as $ka=>$va){
			if($result['status']<=$va['audit_sort']){
				unset($audit_arr[$ka]);
			}
		}
		$this->assign('audit_list',$audit_arr);
		$audit_sh=$this->audit_type_next_person(5,$result['audit_type'],$result['status'],'egt');
		$audit_qs=$this->audit_type_next_person(5,$result['audit_type'],$result['status'],'lt');
		if(in_array($this->login_id,explode(',',$audit_sh['audit_uid']))){$shenhe=1;}
		if(in_array($this->login_id,explode(',',$audit_qs['audit_uid']))){$qishen=1;}
		$this->assign('shenhe',$shenhe);
		$this->assign('qishen',$qishen);
		$this->assign('result',$result);
		$db_task_detail=M('db_task_detail');
		$where_d['mid']=$_REQUEST['id'];
		$result_d=$db_task_detail->where($where_d)->select();
		$ct=0;
		foreach($result_d as $key=>$value){
			if($value['status']==-1){$ct++;}
			$result_d[$key]['username']=$this->get_username($value['uid']);
		}
		$this->assign('ct',$ct);
		$this->assign('result_d',$result_d);
		$this->assign('audit',$_REQUEST['audit']);
		$this->assign('pid',$_REQUEST['pid']);
		$this->assign('id',$_REQUEST['id']);
		//项目详情
		$project=parent::get_project($_REQUEST['pid']?$_REQUEST['pid']:$result['pid']);
		$this->assign('project',$project);
		$this->assign('project_bg',$this->get_project_bg($_REQUEST['pid']));
		$this->display();
	}
	
	//任务管理-->ajax designed by:zhangsen 2017-12-28
	function task_ajax(){
		if($_REQUEST['action']=='delete')
		{
			$db_task=M('db_task');
			$db_audit_main=M('db_audit_main');
			$db_task_detail=M('db_task_detail');
			$where_de['id']=$_REQUEST['id'];
			$db_task->where($where_de)->delete();
			$where_del['audit_id']=$_REQUEST['id'];
			$result_bool=$db_audit_main->where($where_del)->delete();
			$where_deld['mid']=$_REQUEST['id'];
			$db_task_detail->where($where_deld)->delete();
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='add_user')
		{
			$db_task_detail=M('db_task_detail');
			$where['mid']=$_REQUEST['mid'];
			$where['uid']=$_REQUEST['uid'];
			$where['note']=$_REQUEST['note'];
			$where['end_date']=$_REQUEST['end_date'];
			$where['status']=-1;
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_task_detail->add($where);
			if($result_bool)
			{
				$project=$this->get_project($_REQUEST['pid']);
				$user=$this->get_user($_REQUEST['uid']);
				$db_task=M('db_task');
				$db_universal_label1=M('db_universal_label1');
				$db_universal_label2=M('db_universal_label2');
				$where_t['id']=$_REQUEST['mid'];
				$task_result=$db_task->where($where_t)->find();
				//任务分类
				$where_t1['id']=$task_result['type1'];
				$name_t1=$db_universal_label1->where($where_t1)->find();
				$alert_type1=$name_t1['first_name'];
				//任务子类
				$where_t2['id']=$task_result['type2'];
				$name_t2=$db_universal_label2->where($where_t2)->find();
				$alert_type2=$name_t2['second_name'];
				$alert_type=$alert_type1.'-'.$alert_type2;
				if($user['openid']){
					$this->send_task_notice($user['openid'],'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa9104bb1d0267056&redirect_uri=http://ko.joyuti.com/dms_erp/index.php?s=Mobile/Project/task_detail/id/'.$_REQUEST['mid'].'&response_type=code&scope=snsapi_base&state=1#wechat_redirect','您有新的项目任务，请及时查看。',$project['project_num'],$project['project_name'],$alert_type,NULL,'该任务截止日期为'.$_REQUEST['end_date'].'，请及时到系统查看完成，谢谢。');
				}
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='edit_user')
		{
			$db_task_detail=M('db_task_detail');
			$where['id']=$_REQUEST['id'];
			$where['uid']=$_REQUEST['uid'];
			$where['note']=$_REQUEST['note'];
			$where['end_date']=$_REQUEST['end_date'];
			$result_bool=$db_task_detail->save($where);
			if($result_bool)
			{
				$project=$this->get_project($_REQUEST['pid']);
				$user=$this->get_user($_REQUEST['uid']);
				$db_task=M('db_task');
				$db_universal_label1=M('db_universal_label1');
				$db_universal_label2=M('db_universal_label2');
				$where_t['id']=$_REQUEST['mid'];
				$task_result=$db_task->where($where_t)->find();
				//任务分类
				$where_t1['id']=$task_result['type1'];
				$name_t1=$db_universal_label1->where($where_t1)->find();
				$alert_type1=$name_t1['first_name'];
				//任务子类
				$where_t2['id']=$task_result['type2'];
				$name_t2=$db_universal_label2->where($where_t2)->find();
				$alert_type2=$name_t2['second_name'];
				$alert_type=$alert_type1.'-'.$alert_type2;
				if($user['openid']){
					$this->send_task_notice($user['openid'],'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa9104bb1d0267056&redirect_uri=http://ko.joyuti.com/dms_erp/index.php?s=Mobile/Project/task_detail/id/'.$_REQUEST['mid'].'&response_type=code&scope=snsapi_base&state=1#wechat_redirect','您有新的项目任务，请及时查看。',$project['project_num'],$project['project_name'],$alert_type,NULL,'该任务截止日期为'.$_REQUEST['end_date'].'，请及时到系统查看完成，谢谢。');
				}
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='add_task')
		{
			$db_task=M('db_task');
			if($_REQUEST['pid']){
				$where_t['work_type']=2;
			}else{
				$where_t['work_type']=1;
			}
			$where_t['type1']=$_REQUEST['type1'];
			$where_t['type2']=$_REQUEST['type2'];
			$where_t['type3']=$_REQUEST['type3'];
			$where_t['type1_other']=$_REQUEST['type1_other']?$_REQUEST['type1_other']:NULL;
			$where_t['type2_other']=$_REQUEST['type2_other']?$_REQUEST['type2_other']:NULL;
			$where_t['type3_other']=$_REQUEST['type3_other']?$_REQUEST['type3_other']:NULL;
			$where_t['head_uid']=$_REQUEST['head_uid'];
			$where_t['name']=$_REQUEST['name'];
			$where_t['supervision']=$_REQUEST['supervision'];
			$where_t['start_date']=$_REQUEST['start_date'];
			$where_t['end_date']=$_REQUEST['end_date'];
			foreach($_REQUEST['file_name'] as $key=>$value){
				if(!empty($value)){
					$file_name.=$value.',';
				}
			}
			foreach($_REQUEST['file_url'] as $key=>$value){
				if(!empty($value)){
					$file_url.=$value.',';
				}
			}
			$where_t['file_name']=rtrim($file_name,',')?rtrim($file_name,','):NULL;
			$where_t['file_url']=rtrim($file_url,',')?rtrim($file_url,','):NULL;
			$where_t['remark']=$_REQUEST['remark'];
			$audit_id=$db_task->add($where_t);
			//添加
			$db_audit_main=M('db_audit_main');
			$where_j_n['order_number']=array('like','RW'.date('Ymd').'%');
			$judge_num=$db_audit_main->field('order_number')->where($where_j_n)->order('id desc')->select();
			if(empty($judge_num))
			{
				$num='001';
			}
			else
			{
				$num=substr($judge_num[0]['order_number'],10,3)+1;
				if(strlen($num)==1){$num='00'.$num;}
				elseif(strlen($num)==2){$num='0'.$num;}
			}
			$where_a['order_number']='RW'.date('Ymd').$num;
			$where_a['pid']=$_REQUEST['pid']!=-10?$_REQUEST['pid']:NULL;
			$where_a['audit_id']=$audit_id;
			$where_a['audit_type']=130005;
			$where_a['main_demand']=$_REQUEST['text_hid'];
			$where_a['status']=-1;
			$where_a['type']=5;
			$where_a['add_uid']=$this->login_id;
			$where_a['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_audit_main->add($where_a);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='edit_task')
		{
			$db_task=M('db_task');
			$where_t['id']=$_REQUEST['id'];
			foreach($_REQUEST['file_name'] as $key=>$value){
				if(!empty($value)){
					$file_name.=$value.',';
				}
			}
			foreach($_REQUEST['file_url'] as $key=>$value){
				if(!empty($value)){
					$file_url.=$value.',';
				}
			}
			$where_t['type1']=$_REQUEST['type1'];
			$where_t['type2']=$_REQUEST['type2'];
			$where_t['type3']=$_REQUEST['type3'];
			$where_t['type1_other']=$_REQUEST['type1_other']?$_REQUEST['type1_other']:NULL;
			$where_t['type2_other']=$_REQUEST['type2_other']?$_REQUEST['type2_other']:NULL;
			$where_t['type3_other']=$_REQUEST['type3_other']?$_REQUEST['type3_other']:NULL;
			$where_t['head_uid']=$_REQUEST['head_uid'];
			$where_t['file_name']=rtrim($file_name,',')?rtrim($file_name,','):NULL;
			$where_t['file_url']=rtrim($file_url,',')?rtrim($file_url,','):NULL;
			$where_t['name']=$_REQUEST['name'];
			$where_t['supervision']=$_REQUEST['supervision'];
			$where_t['start_date']=$_REQUEST['start_date'];
			$where_t['end_date']=$_REQUEST['end_date'];
			$where_t['remark']=$_REQUEST['remark'];
			$result_bool=$db_task->save($where_t);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='submit_file')
		{
			$db_task_detail=M('db_task_detail');
			$where['id']=$_REQUEST['mid'];
			$where['file_url']=$_REQUEST['file_url'];
			$where['file_name']=$_REQUEST['file_name'];
			$result_bool=$db_task_detail->save($where);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='delete_file')
		{
			$db_task_detail=M('db_task_detail');
			$where['id']=$_REQUEST['id'];
			$where['file_url']=NULL;
			$where['file_name']=NULL;
			$result_bool=$db_task_detail->save($where);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='delete_user')
		{
			$db_task_detail=M('db_task_detail');
			$where['id']=$_REQUEST['id'];
			$result_bool=$db_task_detail->where($where)->delete();
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='end_user')
		{
			$db_task_detail=M('db_task_detail');
			$where['id']=$_REQUEST['mid'];
			$where['score']=$_REQUEST['score'];
			$where['score_comments']=$_REQUEST['score_comments'];
			$where['status']=1;
			$result_bool=$db_task_detail->save($where);
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='submit_task')
		{
			$db_task=M('db_task');
			$where['id']=$_REQUEST['mid'];
			$where['conclusion']=$_REQUEST['conclusion'];
			foreach($_REQUEST['file_name'] as $key=>$value){
				if(!empty($value)){
					$file_name.=$value.',';
				}
			}
			foreach($_REQUEST['file_url'] as $key=>$value){
				if(!empty($value)){
					$file_url.=$value.',';
				}
			}
			$where['file_name_c']=rtrim($file_name,',')?rtrim($file_name,','):NULL;
			$where['file_url_c']=rtrim($file_url,',')?rtrim($file_url,','):NULL;
			$db_task->save($where);
			$db_audit_main=M('db_audit_main');
			$where_m['audit_id']=$_REQUEST['mid'];
			$where_m['type']=5;
			$where_save['status']=1;
			$where_save['post_uid']=$this->login_id;
			$where_save['post_date']=date('Y-m-d H:i:s');
			$result_bool=$db_audit_main->where($where_m)->save($where_save);
			//发送提醒
			$where_f['audit_id']=$_REQUEST['mid'];
			$where_f['type']=5;
			$find_result=$db_audit_main->where($where_f)->find();
			$user_f=$this->get_user($find_result['add_uid']);
			$find_result['add_user']=$user_f['username'];
			$sta=$this->audit_type_next_person(5,$find_result['audit_type'],-1,'egt');
			$audit_arr=explode(',',$sta['audit_uid']);
			foreach($audit_arr as $key=>$value){
				$user=$this->get_user($value);
				if($user['openid']){
					$this->send_audit_notice($user['openid'],'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa9104bb1d0267056&redirect_uri=http://ko.joyuti.com/dms_erp/index.php?s=Mobile/Project/task_detail/id/'.$find_result['audit_id'].'/audit/sh&response_type=code&scope=snsapi_base&state=1#wechat_redirect','您有新的任务管理审核流程，请及时处理','任务管理','申请单号【'.$find_result['order_number'].'】',$find_result['add_user'],substr($find_result['add_date'],0,10),NULL);
				}
			}
			if($result_bool)
			{
				$data['status']=self::YES;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='type2_list')
		{
			$type1=$_REQUEST['type1'];
			$type1_list=$this->library_type_list(18,NULL,NULL,1);
			foreach($type1_list as $key=>$value){
				if($type1==$value['id']){
					$type2_list=$value['second'];
				}
			}
			if($type2_list)
			{
				$data['status']=self::YES;
				$data['data']=$type2_list;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
		elseif($_REQUEST['action']=='type3_list')
		{
			$type1=$_REQUEST['type1'];
			$type2=$_REQUEST['type2'];
			$type1_list=$this->library_type_list(18,NULL,NULL,1);
			foreach($type1_list as $key=>$value){
				if($type1==$value['id']){
					$type2_list=$value['second'];
					foreach($type2_list as $kk=>$vv){
						if($type2==$vv['id']){
							$type3_list=$vv['third'];
						}
					}
				}
			}
			if($type3_list)
			{
				$data['status']=self::YES;
				$data['data']=$type3_list;
			}
			else
			{
				$data['status']=self::NO;
			}
			echo json_encode($data);die;
		}
	}

	//公共方法:客户-->添加 designed by:zhangsen 2017-12-05
	function project_card_add(){
		if($_REQUEST['way']==1){
		}elseif($_REQUEST['way']==2){
			$db_province_city=M('db_province_city');
			$where_p['reid']=0;
			$result_p=$db_province_city->where($where_p)->select();
			$caty_arr=array();
			foreach($result_p as $key=>$value){
				$caty_arr[$key]['value']=$value['id'];
				$caty_arr[$key]['text']=$value['name'];
				$where_c['reid']=$value['id'];
				$result_c=$db_province_city->where($where_c)->select();
				foreach($result_c as $k=>$v){
					$caty_arr[$key]['children'][$k]['value']=$v['id'];
					$caty_arr[$key]['children'][$k]['text']=$v['name'];
				}
			}
			$this->assign('caty_arr',json_encode($caty_arr));
			$db_project_contacts=M('db_project_contacts');
			$where['center_id']=$_REQUEST['center_id'];
			if(!empty($_REQUEST['arr'])){$where['id']=array('not in',$_REQUEST['arr']);}
			$data=$db_project_contacts->where($where)->order('username')->select();
			foreach($data as $key=>$value)
			{
				$data[$key]['province']=$this->get_province_city($value['province_id'],2);
				$data[$key]['city']=$this->get_province_city($value['city_id'],2);
			}
			//添加
			$this->assign('action',$_REQUEST['action']);
			$this->assign('f_pid',$_REQUEST['pid']);
			$this->assign('way',$_REQUEST['way']);
			$this->assign('since',$_REQUEST['since']);
			$this->assign('data',$data);
			$this->assign("province_list",$this->get_province_city());
			$this->display('project_card_add');die;
		}
	}
	
	//公共方法:客户-->添加-->保存 designed by:zhangsen 2017-12-05
	function project_card_doadd(){
		$db_project=M('db_project');
		if($_REQUEST['way']==1){
			//选择
			$db_project_bg=M('db_project_bg');
			$project=$this->get_project($_REQUEST['pid']);
			$search_where['id']=$project['id'];
			if(empty($project['other_cids'])){
				$search_where['other_cids']=$_REQUEST['user'];
				$search_where['other_cids_uid']=$this->login_id;
				$search_where['other_cids_date']=date('Y-m-d H:i:s');
			}else{
				$search_where['other_cids']=$project['other_cids'].",".$_REQUEST['user'];
				$search_where['other_cids_uid']=$project['other_cids_uid'].",".$this->login_id;
				$search_where['other_cids_date']=$project['other_cids_date'].",".date('Y-m-d H:i:s');
			}
			$result_bool=$db_project->save($search_where);
		}elseif($_REQUEST['way']==2){
			//添加
			$project=$this->get_project($_REQUEST['pid']);
			$db_project_contacts=M('db_project_contacts');
			$where_a['center_id']=$project['center_id'];
			if(!empty($_REQUEST['mobilephone']))
			{
				$where_a['mobilephone']=$_REQUEST['mobilephone'];
				$search_a=$db_project_contacts->where($where_a)->find();
				if(!empty($search_a))
				{
					$data['status']=self::NO;
					$data['msg']='同一中心手机号重复，该人员为'.$search_a['username'];
					echo json_encode($data);die;
				}
			}
			if(!empty($_REQUEST['telephone']))
			{
				unset($where_a['mobilephone']);
				$where_a['telephone']=$_REQUEST['telephone'];
				$search_b=$db_project_contacts->where($where_a)->find();
				if(!empty($search_b))
				{
					$data['status']=self::NO;
					$data['msg']='同一中心座机号重复，该人员为'.$search_b['username'];
					echo json_encode($data);die;
				}
			}
			$add_where['center_id']=$project['center_id'];
			$add_where['username']=$_POST['username'];
			$add_where['gender']=$_POST['gender'];
			if(!empty($_POST['company_name'])){$add_where['company_name']=$_POST['company_name'];}
			if(!empty($_POST['job'])){$add_where['job']=$_POST['job'];}
			if(!empty($_POST['mobilephone'])){$add_where['mobilephone']=$_POST['mobilephone'];}
			if(!empty($_POST['telephone'])){$add_where['telephone']=$_POST['telephone'];}
			if(!empty($_POST['qq'])){$add_where['qq']=$_POST['qq'];}
			if(!empty($_POST['email'])){$add_where['email']=$_POST['email'];}
			if(!empty($_POST['postcode'])){$add_where['postcode']=$_POST['postcode'];}
			$add_where['province_id']=$_POST['province_id'];
			$add_where['city_id']=$_POST['city_id'];
			if(!empty($_POST['address'])){$add_where['address']=$_POST['address'];}
			if(!empty($_POST['remark'])){$add_where['remark']=$_POST['remark'];}
			$add_where['add_uid']=$this->login_id;
			$add_where['add_user']=$this->login_username;
			$add_where['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_project_contacts->add($add_where);
			//传出该中心的所有客户
			$where['center_id']=$project['center_id'];
			$customers_result=$db_project_contacts->where($where)->order('username')->select();
			foreach($customers_result as $key=>$value)
			{
				if(!empty($value['province_id'])){$customers_result[$key]['province']=$this->get_province_city($value['province_id'],2);}
				if(!empty($value['city_id'])){$customers_result[$key]['city']=$this->get_province_city($value['city_id'],2);}
			}
			$data['id']=$result_bool;
			$Firstletter = new \Think\Firstletter();
			$data['py']=$Firstletter->conv(mb_substr($_POST['username'],0,1,'utf-8'));;
			$where_c['id']=$result_bool;
			$data['cus']=$db_project_contacts->where($where_c)->find();
			$data['customers']=$customers_result;
		}
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}
	
	//公共方法:客户-->编辑-->保存 designed by:zhaoshihao 2017-05-10
	function project_card_doedit(){//dump($_REQUEST);die;
		//判断是否重复
		$project=$this->get_project($_REQUEST['pid']);
		$db_project_contacts=M('db_project_contacts');
		if(!empty($_REQUEST['id'])){$where_a['_string']="id !=".$_REQUEST['id'];}
		$where_a['center_id']=$project['center_id'];
		if(!empty($_REQUEST['mobilephone']))
		{
			$where_a['mobilephone']=$_REQUEST['mobilephone'];
			$search_a=$db_project_contacts->where($where_a)->find();
			if(!empty($search_a))
			{
				$data['status']=self::NO;
				$data['msg']='同一中心手机号重复，该人员为'.$search_a['username'];
				echo json_encode($data);die;
			}
		}
		if(!empty($_REQUEST['telephone']))
		{
			unset($where_a['mobilephone']);
			$where_a['telephone']=$_REQUEST['telephone'];
			$search_b=$db_project_contacts->where($where_a)->find();
			if(!empty($search_b))
			{
				$data['status']=self::NO;
				$data['msg']='同一中心座机号重复，该人员为'.$search_b['username'];
				echo json_encode($data);die;
			}
		}
		$save_where['id']=$_REQUEST['id'];
		$save_where['username']=$_REQUEST['username'];
		$save_where['gender']=$_REQUEST['gender'];
		if(!empty($_REQUEST['mobilephone'])){$save_where['mobilephone']=$_REQUEST['mobilephone'];}else{$save_where['mobilephone']=NULL;}
		if(!empty($_REQUEST['telephone'])){$save_where['telephone']=$_REQUEST['telephone'];}else{$save_where['telephone']=NULL;}
		if(!empty($_REQUEST['job'])){$save_where['job']=$_REQUEST['job'];}else{$save_where['job']=NULL;}
		if(!empty($_REQUEST['email'])){$save_where['email']=$_REQUEST['email'];}else{$save_where['email']=NULL;}
		if(!empty($_REQUEST['qq'])){$save_where['qq']=$_REQUEST['qq'];}else{$save_where['qq']=NULL;}
		if(!empty($_REQUEST['remark'])){$save_where['remark']=$_REQUEST['remark'];}else{$save_where['remark']=NULL;}
		$save_where['province_id']=$_REQUEST['province_id'];
		$save_where['city_id']=$_REQUEST['city_id'];
		if(!empty($_REQUEST['address'])){$save_where['address']=$_REQUEST['address'];}else{$save_where['address']=NULL;}
		if(!empty($_REQUEST['company_name'])){$save_where['company_name']=$_REQUEST['company_name'];}else{$save_where['company_name']=NULL;}
		$result_bool=$db_project_contacts->save($save_where);
		$data['status']=self::YES;
		$data['msg']='操作成功';
		echo json_encode($data);die;
	}
	
	//公共方法:客户-->编辑 designed by:zhangsen 2017-12-05
	function project_card_edit(){
		$db_province_city=M('db_province_city');
		$where_p['reid']=0;
		$result_p=$db_province_city->where($where_p)->select();
		$caty_arr=array();
		foreach($result_p as $key=>$value){
			$caty_arr[$key]['value']=$value['id'];
			$caty_arr[$key]['text']=$value['name'];
			$where_c['reid']=$value['id'];
			$result_c=$db_province_city->where($where_c)->select();
			foreach($result_c as $k=>$v){
				$caty_arr[$key]['children'][$k]['value']=$v['id'];
				$caty_arr[$key]['children'][$k]['text']=$v['name'];
			}
		}
		$this->assign('f_pid',$_REQUEST['pid']);
		$this->assign('id',$_REQUEST['id']);
		$this->assign('caty_arr',json_encode($caty_arr));
		//客户详情
		$db_project_contacts=M('db_project_contacts');
		$db_project=M('db_project');
		$card_where['id']=$_REQUEST['id'];
		$info=$db_project_contacts->where($card_where)->find();
		$data=$info;
		//能否编辑名字
		$where_stage['_string']='db_project.cid="'.$_REQUEST['id'].'"';
		$customers_result=$db_project->field('db_project.id,db_project.project_num,db_project.project_name,db_project.manager_uid')->where($where_stage)->group('db_project.id')->select();
		if(count($customers_result)>1){$data['no_edit']=1;}
		if(!empty($data['province_id'])){$data['province']=$this->get_province_city($data['province_id'],2);}
		if(!empty($data['city_id'])){$data['city']=$this->get_province_city($data['city_id'],2);}
		$this->assign('data',$data);
		$this->display();die;
	}
	
	//公共方法:项目客户-->详情 designed by:zhangsen 2017-12-05
	function project_card_detail(){
		$db_project_contacts=M('db_project_contacts');
		$where['id']=$_REQUEST['id'];
		$data=$db_project_contacts->where($where)->find();
		if(!empty($data['province_id'])){$data['province']=$this->get_province_city($data['province_id'],2);}
		if(!empty($data['city_id'])){$data['city']=$this->get_province_city($data['city_id'],2);}
		echo json_encode($data);die;
	}
	
	//公共方法:获取客户列表 designed by:zhangsen 2017-12-04
	function project_card_ajax(){
		$Firstletter = new \Think\Firstletter();
		//项目详情
		$project=parent::get_project($_REQUEST['id']);
		//项目客户下拉菜单
		$db_project_contacts=M('db_project_contacts');
		$where_all['center_id']=$project['center_id'];
		$customers_result=$db_project_contacts->where($where_all)->order('username')->select();
		foreach($customers_result as $key=>$value)
		{
			if(!empty($value['province_id'])){$customers_result[$key]['province']=$this->get_province_city($value['province_id'],2);}
			if(!empty($value['city_id'])){$customers_result[$key]['city']=$this->get_province_city($value['city_id'],2);}
			$customers_result[$key]['first_letter']=$Firstletter->conv(mb_substr($value['username'],0,1,'utf-8'));
		}
		foreach($customers_result as $key=>$value){
			$customers[$value['first_letter']][]=$value;
		}
		ksort($customers);
		echo json_encode($customers);die;
	}
}
?>